Skip to content

Commit

Permalink
Use pkgconfig to expose libraries (#21)
Browse files Browse the repository at this point in the history
* Use pkg-config to find installed bmi-fortran spec

* Add pkg-config files for heatf model and its BMI

* Use CMAKE_CURRENT_BINARY_DIR instead of CMAKE_BINARY_DIR

* Require bmif library

* Add pkg-config dependency to test environment

* Use LIBRARY_PREFIX env variable on Windows

* Test for pkg-config files; run ctest

* Make SHLIB_EXT env variable for unix-based systems

* Test for installed files

* Remove duplicate sample configuration file

* List pkg-config requirement
  • Loading branch information
mdpiper authored Mar 13, 2024
1 parent 6909f22 commit 93a3157
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 39 deletions.
47 changes: 33 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
run:
shell: bash -l {0}

env:
SHLIB_EXT: ${{ matrix.os == 'ubuntu-latest' && '.so' || '.dylib' }}

strategy:
matrix:
os: [ubuntu-latest, macos-latest]
Expand All @@ -28,6 +31,7 @@ jobs:
environment-name: testing
create-args: >-
cmake
pkg-config
fortran-compiler
bmi-fortran
Expand All @@ -45,7 +49,16 @@ jobs:
working-directory: ${{ github.workspace }}/build
run: cmake --build . --target install --config ${{ matrix.build-type }}

- name: Test
- name: Test for installed files
run: |
test -h $CONDA_PREFIX/lib/libheatf${{ env.SHLIB_EXT }}
test -f $CONDA_PREFIX/include/heatf.mod
test -f $CONDA_PREFIX/lib/pkgconfig/heatf.pc
test -h $CONDA_PREFIX/lib/libbmiheatf${{ env.SHLIB_EXT }}
test -f $CONDA_PREFIX/include/bmiheatf.mod
test -f $CONDA_PREFIX/lib/pkgconfig/bmiheatf.pc
- name: Run CTest
working-directory: ${{ github.workspace }}/build
run: ctest -C ${{ matrix.build-type }} --output-on-failure

Expand All @@ -70,6 +83,9 @@ jobs:

runs-on: windows-latest

env:
LIBRARY_PREFIX: $env:CONDA_PREFIX\Library

steps:
- uses: actions/checkout@v4
- uses: ilammy/msvc-dev-cmd@v1
Expand All @@ -79,6 +95,7 @@ jobs:
environment-name: testing
create-args: >-
cmake
pkg-config
cxx-compiler
init-shell: >-
powershell
Expand All @@ -94,7 +111,7 @@ jobs:
unzip bmi-fortran.zip
cd bmi-fortran-master
mkdir build && cd build
cmake .. -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX="$env:CONDA_PREFIX" -DCMAKE_BUILD_TYPE=Release
cmake .. -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX="${{ env.LIBRARY_PREFIX }}" -DCMAKE_BUILD_TYPE=Release
cmake --build . --target install --config Release
cd ${{ github.workspace }}
Expand All @@ -104,18 +121,20 @@ jobs:
- name: Configure, build, and install
working-directory: ${{ github.workspace }}/build
run: |
cmake .. -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX="$env:CONDA_PREFIX" -DCMAKE_BUILD_TYPE=Release
cmake .. -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX="${{ env.LIBRARY_PREFIX }}" -DCMAKE_BUILD_TYPE=Release
cmake --build . --target install --config Release
- name: Test installed files
- name: Test for installed files
run: |
if ( -not ( Test-Path -Path $env:CONDA_PREFIX\lib\libheatf.a ) ){ exit 1 }
if ( -not ( Test-Path -Path $env:CONDA_PREFIX\bin\run_heatf.exe ) ){ exit 1 }
if ( -not ( Test-Path -Path $env:CONDA_PREFIX\include\heatf.mod ) ){ exit 1 }
if ( -not ( Test-Path -Path $env:CONDA_PREFIX\bin\run_bmiheatf.exe ) ){ exit 1 }
if ( -not ( Test-Path -Path $env:CONDA_PREFIX\lib\libbmiheatf.a ) ){ exit 1 }
if ( -not ( Test-Path -Path $env:CONDA_PREFIX\include\bmiheatf.mod ) ){ exit 1 }
# - name: Unit test with CTest
# working-directory: ${{ github.workspace }}/build
# run: ctest -C Release -VV --output-on-failure
if ( -not ( Test-Path -Path ${{ env.LIBRARY_PREFIX }}\lib\libheatf.a ) ){ exit 1 }
if ( -not ( Test-Path -Path ${{ env.LIBRARY_PREFIX }}\bin\run_heatf.exe ) ){ exit 1 }
if ( -not ( Test-Path -Path ${{ env.LIBRARY_PREFIX }}\include\heatf.mod ) ){ exit 1 }
if ( -not ( Test-Path -Path ${{ env.LIBRARY_PREFIX }}\lib\pkgconfig\heatf.pc ) ){ exit 1 }
if ( -not ( Test-Path -Path ${{ env.LIBRARY_PREFIX }}\bin\run_bmiheatf.exe ) ){ exit 1 }
if ( -not ( Test-Path -Path ${{ env.LIBRARY_PREFIX }}\lib\libbmiheatf.a ) ){ exit 1 }
if ( -not ( Test-Path -Path ${{ env.LIBRARY_PREFIX }}\include\bmiheatf.mod ) ){ exit 1 }
if ( -not ( Test-Path -Path ${{ env.LIBRARY_PREFIX }}\lib\pkgconfig\bmiheatf.pc ) ){ exit 1 }
- name: Run CTest
working-directory: ${{ github.workspace }}/build
run: ctest -C Release -VV --output-on-failure
24 changes: 7 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,16 @@ include(GNUInstallDirs)
set(model_name heatf)
set(bmi_name bmi${model_name})

# Determine the Fortran BMI version.
if(DEFINED ENV{BMIF_VERSION})
set(bmif_version $ENV{BMIF_VERSION})
else()
set(bmif_version "2.0")
endif()
string(REPLACE "." "_" bmif_module_version ${bmif_version})
message("-- BMIF version - ${bmif_version}")
message("-- BMIF module version - ${bmif_module_version}")
find_package(PkgConfig REQUIRED)
pkg_check_modules(BMIF REQUIRED IMPORTED_TARGET bmif)
string(REPLACE "." "_" bmif_module_version ${BMIF_VERSION})
message("-- bmif module version - ${bmif_module_version}")
message("-- bmif library path - ${BMIF_LINK_LIBRARIES}")
message("-- bmif include dir - ${BMIF_INCLUDE_DIRS}")
include_directories(${BMIF_INCLUDE_DIRS})

set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/mod)

# Locate the installed Fortran BMI bindings (bmif library and module file)
# through CMAKE_PREFIX_PATH.
find_library(bmif_lib bmif)
find_path(bmif_inc bmif_${bmif_module_version}.mod)
include_directories(${bmif_inc})
message("-- bmif_lib - ${bmif_lib}")
message("-- bmif_inc - ${bmif_inc}")

add_subdirectory(heat)
add_subdirectory(bmi_heat)
add_subdirectory(test)
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ This example can be built on Linux, macOS, and Windows.
in that repository. You can choose to build them from source or
install them through a conda binary. If using fpm, the binding
will be automatically downloaded and built for you.
* pkg-config

### CMake - Linux and macOS

To build this example from source with CMake,
using the current Fortran BMI version, run

export BMIF_VERSION=2.0
mkdir _build && cd _build
cmake .. -DCMAKE_INSTALL_PREFIX=<path-to-installation>
make
Expand Down Expand Up @@ -80,12 +80,15 @@ The installation will look like
| |-- bmiheatf.mod
| `-- heatf.mod
`-- lib
|-- libbmif.2.0.dylib
|-- libbmif.dylib -> libbmif.2.0.dylib
|-- libbmif.a
|-- libbmif.2.0.2.dylib
|-- libbmif.dylib -> libbmif.2.0.2.dylib
|-- libbmiheatf.dylib
`-- libheatf.dylib

3 directories, 9 files
|-- libheatf.dylib
`-- pkgconfig
|-- bmif.pc
|-- bmiheatf.pc
`-- heatf.pc
```

From the build directory,
Expand All @@ -103,7 +106,6 @@ To configure this example from source with cmake
using the current Fortran BMI version,
run the following in a [Developer Command Prompt](https://docs.microsoft.com/en-us/dotnet/framework/tools/developer-command-prompt-for-vs)

set "BMIF_VERSION=2.0"
mkdir _build && cd _build
cmake .. ^
-G "NMake Makefiles" ^
Expand Down
12 changes: 11 additions & 1 deletion bmi_heat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# bmi-heat

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/${bmi_name}.pc.cmake
${CMAKE_CURRENT_BINARY_DIR}/${bmi_name}.pc
@ONLY
)

# Create shared library, except on Windows.
if(WIN32)
add_library(${bmi_name} bmi_heat.f90)
else()
add_library(${bmi_name} SHARED bmi_heat.f90)
endif()
target_link_libraries(${bmi_name} ${model_name} ${bmif_lib})
target_link_libraries(${bmi_name} ${model_name} ${BMIF_LINK_LIBRARIES})

add_executable(run_${bmi_name} bmi_main.f90)
target_link_libraries(run_${bmi_name} ${bmi_name})
Expand All @@ -25,3 +31,7 @@ install(
FILES ${CMAKE_Fortran_MODULE_DIRECTORY}/${bmi_name}.mod
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${bmi_name}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
11 changes: 11 additions & 0 deletions bmi_heat/bmiheatf.pc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: @bmi_name@
Description: BMI for the heatf model
Version: @CMAKE_PROJECT_VERSION@
Requires: @model_name@, @BMIF_LIBRARIES@
Libs: -L${libdir} -l@bmi_name@
Cflags: -I${includedir}
10 changes: 10 additions & 0 deletions heat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# heat

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/${model_name}.pc.cmake
${CMAKE_CURRENT_BINARY_DIR}/${model_name}.pc
@ONLY
)

# Create shared library, except on Windows.
if(WIN32)
add_library(${model_name} heat.f90)
Expand All @@ -24,3 +30,7 @@ install(
FILES ${CMAKE_Fortran_MODULE_DIRECTORY}/${model_name}.mod
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/${model_name}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
10 changes: 10 additions & 0 deletions heat/heatf.pc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: @model_name@
Description: The two-dimensional heat equation in Fortran
Version: @CMAKE_PROJECT_VERSION@
Libs: -L${libdir} -l@model_name@
Cflags: -I${includedir}
Empty file removed sample.cfg
Empty file.

0 comments on commit 93a3157

Please sign in to comment.