From f61033f2f9acf5a6daf0e573aad732046d783cf9 Mon Sep 17 00:00:00 2001 From: scivision Date: Tue, 21 Feb 2023 22:04:34 -0500 Subject: [PATCH 1/3] modernize/simplify CMake and CI --- .github/workflows/continuous.yml | 40 ++++------------------- .github/workflows/nightly.yml | 40 ++++------------------- .gitignore | 1 - CMakeLists.txt | 55 ++++++++++++++------------------ tests/CMakeLists.txt | 12 ++++--- 5 files changed, 46 insertions(+), 102 deletions(-) diff --git a/.github/workflows/continuous.yml b/.github/workflows/continuous.yml index 037229b..22058b3 100644 --- a/.github/workflows/continuous.yml +++ b/.github/workflows/continuous.yml @@ -11,44 +11,18 @@ jobs: # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix runs-on: ${{ matrix.os }} strategy: - fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] config: [Release, Debug, RelWithDebInfo] - include: - - os: macos-latest - name: macOS - - os: ubuntu-latest - name: Linux - - os: windows-latest - name: Windows - steps: - - uses: actions/checkout@v2 - - name: Create Build Environment - # Some projects don't allow in-source building, so create a separate build directory - # We'll use this as our working directory for all subsequent commands - run: cmake -E make_directory ${{github.workspace}}/build + steps: + - uses: actions/checkout@v3 - name: Configure CMake - # Use a bash shell so we can use the same syntax for environment variable - # access regardless of the host operating system - shell: bash - working-directory: ${{github.workspace}}/build - # Note the current convention is to use the -S and -B options here to specify source - # and build directories, but this is only available with CMake 3.13 and higher. - # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DSTLBFGS_UNIT_TESTS=ON + run: cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DSTLBFGS_UNIT_TESTS=ON - - name: Build - working-directory: ${{github.workspace}}/build - shell: bash - # Execute the build. You can specify a specific target with "--target " - run: cmake --build . --config ${{ matrix.config }} + - name: CMake Build + run: cmake --build build --parallel - - name: Test - working-directory: ${{github.workspace}}/build/tests - shell: bash - # Execute tests defined by the CMake configuration. - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest -C $BUILD_TYPE + - name: CTest + run: ctest --test-dir build diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index e0cfa4f..d7ff3a3 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -13,44 +13,18 @@ jobs: # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix runs-on: ${{ matrix.os }} strategy: - fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] config: [Release, Debug, RelWithDebInfo] - include: - - os: macos-latest - name: macOS - - os: ubuntu-latest - name: Linux - - os: windows-latest - name: Windows - steps: - - uses: actions/checkout@v2 - - name: Create Build Environment - # Some projects don't allow in-source building, so create a separate build directory - # We'll use this as our working directory for all subsequent commands - run: cmake -E make_directory ${{github.workspace}}/build + steps: + - uses: actions/checkout@v3 - name: Configure CMake - # Use a bash shell so we can use the same syntax for environment variable - # access regardless of the host operating system - shell: bash - working-directory: ${{github.workspace}}/build - # Note the current convention is to use the -S and -B options here to specify source - # and build directories, but this is only available with CMake 3.13 and higher. - # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DSTLBFGS_UNIT_TESTS=ON + run: cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DSTLBFGS_UNIT_TESTS=ON - - name: Build - working-directory: ${{github.workspace}}/build - shell: bash - # Execute the build. You can specify a specific target with "--target " - run: cmake --build . --config ${{ matrix.config }} + - name: CMake Build + run: cmake --build build --parallel - - name: Test - working-directory: ${{github.workspace}}/build/tests - shell: bash - # Execute tests defined by the CMake configuration. - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest -C $BUILD_TYPE + - name: CTest + run: ctest --test-dir build diff --git a/.gitignore b/.gitignore index e151175..e7683df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -build *~ *.bak diff --git a/CMakeLists.txt b/CMakeLists.txt index 8360c09..b7fb221 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,50 +1,43 @@ -cmake_minimum_required(VERSION 3.14) -project(stlbfgs) +cmake_minimum_required(VERSION 3.14...3.26) -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) +get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) +if(NOT is_multi_config AND NOT (CMAKE_BUILD_TYPE OR DEFINED ENV{CMAKE_BUILD_TYPE})) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Release default") endif() +project(stlbfgs LANGUAGES CXX) + +enable_testing() + +option(STLBFGS_UNIT_TESTS "STLBFGS_UNIT_TESTS" OFF) + set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(OpenMP) -if(OPENMP_FOUND) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") +set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS true) + +find_package(OpenMP COMPONENTS CXX) + +if(MSVC) + add_compile_options(/W4) +elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU|Intel") + add_compile_options(-Wall -Wextra -Wpedantic) endif() set(RELATIVE_BIN_DIR bin/) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${RELATIVE_BIN_DIR}/) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) - -if (MSVC) - # warning level 4 (and all warnings as errors, /WX) - add_compile_options(/W4) -else() - # lots of warnings and all warnings as errors - add_compile_options(-Wall -Wextra -pedantic) -endif() add_library(stlbfgs linesearch.h stlbfgs.h linesearch.cpp stlbfgs.cpp) +target_link_libraries(stlbfgs PRIVATE $<$:OpenMP::OpenMP_CXX>) +target_include_directories(stlbfgs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +set_property(TARGET stlbfgs PROPERTY SOVERSION 1.0) -if(OpenMP_CXX_FOUND) - target_link_libraries(stlbfgs OpenMP::OpenMP_CXX) -endif() -set_target_properties(stlbfgs PROPERTIES SOVERSION 1.0) - -option(STLBFGS_UNIT_TESTS "STLBFGS_UNIT_TESTS" OFF) if (STLBFGS_UNIT_TESTS) - include(CTest) - enable_testing() - add_subdirectory(tests) + add_subdirectory(tests) endif() add_executable(stlbfgs-helloworld helloworld.cpp) -target_link_libraries(stlbfgs-helloworld ${CMAKE_DL_LIBS} stlbfgs) -if (NOT WIN32) - target_link_libraries(stlbfgs-helloworld m) -endif() +target_link_libraries(stlbfgs-helloworld ${CMAKE_DL_LIBS} stlbfgs $<$>:m>) +file(GENERATE OUTPUT .gitignore CONTENT "*") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 76b5b72..d5a82d6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,10 +1,15 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) - Include(FetchContent) + +set(FETCHCONTENT_QUIET false) + FetchContent_Declare( Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git - GIT_TAG v3.3.1) + GIT_TAG v3.3.1 + GIT_SHALLOW true + TLS_VERIFY ON + INACTIVITY_TIMEOUT 60 + ) FetchContent_MakeAvailable(Catch2) @@ -16,4 +21,3 @@ target_link_libraries(stlbfgs-test-all PRIVATE ${CMAKE_DL_LIBS} stlbfgs Catch2:: include(Catch) catch_discover_tests(stlbfgs-test-all) - From 3f2f70264e759be91dd0bd168bc55108b41f13af Mon Sep 17 00:00:00 2001 From: scivision Date: Tue, 21 Feb 2023 22:14:58 -0500 Subject: [PATCH 2/3] ci: more meaningful tests --- .github/workflows/continuous.yml | 46 ++++++++++++++++++++++++-------- .github/workflows/nightly.yml | 35 +++++++++++++++++------- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/.github/workflows/continuous.yml b/.github/workflows/continuous.yml index 22058b3..774d09e 100644 --- a/.github/workflows/continuous.yml +++ b/.github/workflows/continuous.yml @@ -1,19 +1,21 @@ -name: Build +name: Continuous on: [push] jobs: - Continuous: - name: ${{ matrix.name }} (${{ matrix.config }}) - # The CMake configure and build commands are platform agnostic and should work equally - # well on Windows or Mac. You can convert this to a matrix build if you need - # cross-platform coverage. - # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix - runs-on: ${{ matrix.os }} + + linux-mac: strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - config: [Release, Debug, RelWithDebInfo] + cfg: [{os: ubuntu-latest, cxx: g++-12}, + {os: ubuntu-latest, cxx: g++-9}, + {os: macos-latest, cxx: clang++}] + config: [Release, Debug] + + runs-on: ${{ matrix.cfg.os }} + + env: + CXX: ${{ matrix.cfg.cxx }} steps: - uses: actions/checkout@v3 @@ -25,4 +27,26 @@ jobs: run: cmake --build build --parallel - name: CTest - run: ctest --test-dir build + run: ctest --test-dir build -V + + + windows-msvc: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v3 + + - name: Configure CMake + run: cmake -B build -DSTLBFGS_UNIT_TESTS=ON + + - name: Release build + run: cmake --build build --parallel --config Release + + - name: Release CTest + run: ctest --test-dir build -C Release -V + + - name: Debug build + run: cmake --build build --parallel --config Debug + + - name: Debug CTest + run: ctest --test-dir build -C Debug -V diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index d7ff3a3..162e340 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -5,17 +5,12 @@ on: - cron: '0 4 * * *' jobs: - Continuous: - name: ${{ matrix.name }} (${{ matrix.config }}) - # The CMake configure and build commands are platform agnostic and should work equally - # well on Windows or Mac. You can convert this to a matrix build if you need - # cross-platform coverage. - # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + core: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - config: [Release, Debug, RelWithDebInfo] + os: [ubuntu-latest, macos-latest] + config: [Release, Debug] steps: - uses: actions/checkout@v3 @@ -27,4 +22,26 @@ jobs: run: cmake --build build --parallel - name: CTest - run: ctest --test-dir build + run: ctest --test-dir build -V + + + windows-msvc: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v3 + + - name: Configure CMake + run: cmake -B build -DSTLBFGS_UNIT_TESTS=ON + + - name: Release build + run: cmake --build build --parallel --config Release + + - name: Release CTest + run: ctest --test-dir build -C Release -V + + - name: Debug build + run: cmake --build build --parallel --config Debug + + - name: Debug CTest + run: ctest --test-dir build -C Debug -V From 33da255dd6c3b13e35641ea9682276200bc4c3ab Mon Sep 17 00:00:00 2001 From: scivision Date: Tue, 21 Feb 2023 22:25:30 -0500 Subject: [PATCH 3/3] doc [skip ci] --- README.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e21b4cf..a0a5d09 100644 --- a/README.md +++ b/README.md @@ -34,14 +34,11 @@ int main() { # Build and run unit tests: ```sh -git clone https://github.com/ssloy/stlbfgs.git && -cd stlbfgs && -mkdir build && -cd build && -cmake -DSTLBFGS_UNIT_TESTS:BOOL=ON .. && -cmake --build . -j && -cd tests && -ctest +git clone https://github.com/ssloy/stlbfgs.git +cd stlbfgs +cmake -B build -DSTLBFGS_UNIT_TESTS:BOOL=ON +cmake --build build -j +ctest --test-dir build -V ``` # References @@ -57,4 +54,3 @@ ctest **[5]** Gilbert JC, Lemaréchal C. The module M1QN3. INRIA Rep., version. 2006;3:21. **[6]** Gilbert JC, Lemaréchal C. Some numerical experiments with variable-storage quasi-Newton algorithms. Mathematical Programming 45, pp. 407-435, 1989. -