Skip to content

Commit

Permalink
Update CMake module for finding argp
Browse files Browse the repository at this point in the history
Also test MacOS builds using brew dependencies
  • Loading branch information
leonlynch committed Dec 5, 2022
1 parent f00ee2b commit d0196db
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/macos-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,31 @@ jobs:
fail-fast: false
matrix:
include:
- { name: "MacOS 11", os: macos-11, osx_arch: "x86_64;arm64", build_type: "Debug" }
- { name: "MacOS 11", os: macos-11, osx_arch: "x86_64;arm64", build_type: "Release" }
- { name: "MacOS 12", os: macos-12, osx_arch: "x86_64;arm64", build_type: "Debug" }
- { name: "MacOS 12", os: macos-12, osx_arch: "x86_64;arm64", build_type: "Release" }
- { name: "MacOS 11", os: macos-11, osx_arch: "x86_64;arm64", build_type: "Debug", fetch_deps: YES }
- { name: "MacOS 11", os: macos-11, osx_arch: "x86_64", build_type: "Release", fetch_deps: NO }
- { name: "MacOS 12", os: macos-12, osx_arch: "x86_64", build_type: "Debug", fetch_deps: NO }
- { name: "MacOS 12", os: macos-12, osx_arch: "x86_64;arm64", build_type: "Release", fetch_deps: YES }

name: ${{ matrix.name }} build (static/${{ matrix.build_type }})
runs-on: ${{ matrix.os }}

steps:
- name: Install dependencies
# Homebrew doesn't support universal binaries so only install dependencies for x86_64 builds
if: ${{ matrix.fetch_deps == 'NO' }}
run: |
brew install mbedtls@2
brew install argp-standalone
echo "MbedTLS_ROOT=$(brew --prefix mbedtls@2)" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
- run: git describe --always --dirty

- name: Configure CMake
run: cmake -B build -DCMAKE_OSX_ARCHITECTURES="${{ matrix.osx_arch }}" -DCMAKE_BUILD_TYPE="${{ matrix.build_type }}" -DFETCH_MBEDTLS=YES -DFETCH_ARGP=YES
run: cmake -B build -DCMAKE_OSX_ARCHITECTURES="${{ matrix.osx_arch }}" -DCMAKE_BUILD_TYPE="${{ matrix.build_type }}" -DFETCH_MBEDTLS=${{ matrix.fetch_deps }} -DFETCH_ARGP=${{ matrix.fetch_deps }} -DMbedTLS_ROOT=${{ env.MbedTLS_ROOT }}

- name: Build
run: cmake --build build
Expand Down
61 changes: 37 additions & 24 deletions cmake/Modules/Findargp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,72 @@ include(CheckIncludeFile)
include(CheckSymbolExists)
include(FindPackageHandleStandardArgs)

# Check whether libc provides argp_parse
check_function_exists("argp_parse" argp_parse_FOUND)

# Check for argp.h header
# Check whether system provides argp.h header
CHECK_INCLUDE_FILE(
argp.h
argp_header_FOUND
)

if(argp_header_FOUND)
if(NOT argp_header_FOUND)
# Search for argp.h header elsewhere
find_path(argp_INCLUDE_DIR
NAMES argp.h
)

if(argp_INCLUDE_DIR)
# Ensure that compiler can use argp.h header
unset(argp_header_FOUND CACHE)
set(CMAKE_REQUIRED_INCLUDES "${argp_INCLUDE_DIR}")
CHECK_INCLUDE_FILE(
argp.h
argp_header_FOUND
)
endif()
endif()

# Check whether libc provides argp_parse
check_function_exists(argp_parse argp_parse_FOUND)

if(NOT arg_parse_FOUND)
# Search for argp library
find_library(argp_LIBRARY
NAMES argp
)

if(argp_LIBRARY AND NOT argp_parse_FOUND)
if(argp_LIBRARY)
# Ensure that compiler can use argp library
unset(argp_parse_FOUND CACHE)
set(CMAKE_REQUIRED_LIBRARIES ${argp_LIBRARY})
check_symbol_exists("argp_parse" "argp.h" argp_parse_FOUND)
check_symbol_exists(argp_parse "argp.h" argp_parse_FOUND)
endif()
endif()

if(argp_parse_FOUND)
if(argp_LIBRARY)
set(argp_FOUND_MSG ${argp_LIBRARY})
else()
set(argp_FOUND_MSG "provided by libc")
endif()
if(argp_header_FOUND AND argp_parse_FOUND)
if(argp_LIBRARY)
set(argp_FOUND_MSG ${argp_LIBRARY})
else()
set(argp_FOUND_MSG "provided by libc")
endif()
endif()

find_package_handle_standard_args(argp
REQUIRED_VARS
argp_FOUND_MSG # NOTE: argp_LIBRARY is not required
argp_FOUND_MSG # NOTE: argp_INCLUDE_DIR and argp_LIBRARY are not required
argp_parse_FOUND
argp_header_FOUND
argp_INCLUDE_DIR
)

if(argp_FOUND)
set(argp_INCLUDE_DIRS ${argp_INCLUDE_DIR})
set(argp_LIBRARIES ${argp_LIBRARY})

if(NOT TARGET libargp::argp)
if(argp_LIBRARY)
add_library(libargp::argp UNKNOWN IMPORTED)
set_target_properties(libargp::argp
PROPERTIES
IMPORTED_LOCATION "${argp_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${argp_INCLUDE_DIR}"
)
endif()
if(argp_LIBRARY AND NOT TARGET libargp::argp)
add_library(libargp::argp UNKNOWN IMPORTED)
set_target_properties(libargp::argp
PROPERTIES
IMPORTED_LOCATION "${argp_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${argp_INCLUDE_DIR}"
)
endif()
endif()

Expand Down

0 comments on commit d0196db

Please sign in to comment.