Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Findwgrib2.cmake #55

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Modules/Findwgrib2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This module produces the target wgrib2::wgrib2

find_path(WGRIB2_INCLUDES wgrib2api.mod)
find_library(WGRIB2_LIB libwgrib2.a)
find_library(WGRIB2_API_LIB libwgrib2_api.a)

add_library(wgrib2::wgrib2 UNKNOWN IMPORTED)

# Library builds are different between CMake build and the make build.
# libwgrib2_api.a is only necessary in the CMake build and must come first when linking
if(WGRIB2_API_LIB)
# CMake build. Need both.
set(first_lib ${WGRIB2_API_LIB})
set(second_lib ${WGRIB2_LIB})
else()
# Makefile build. Only need libwgrib2.a
set(FIRST_LIB ${WGRIB2_LIB})
set(second_lib "")
endif()

set_target_properties(wgrib2::wgrib2 PROPERTIES
IMPORTED_LOCATION "${first_lib}"
INTERFACE_INCLUDE_DIRECTORIES "${WGRIB2_INCLUDES}"
INTERFACE_LINK_LIBRARIES "${second_lib}")

set(WGRIB2_LIBRARIES "${first_lib}" "${second_lib}")

find_program(WGRIB2_EXE wgrib2)
execute_process(COMMAND ${WGRIB2_EXE} --version OUTPUT_VARIABLE version_str)

# Wgrib2 changed how it output --version from "v0.x.y.z" to "vx.y.z" starting in wgrib2 3.0
if(version_str MATCHES "^v0.*")
string(SUBSTRING "${version_str}" 3 5 version)
else()
string(SUBSTRING "${version_str}" 1 5 version)
endif()

find_package_handle_standard_args(wgrib2
REQUIRED_VARS WGRIB2_LIBRARIES WGRIB2_INCLUDES WGRIB2_EXE
VERSION_VAR version
)