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

CMake and testability improvements #308

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 16 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

name: CI

on: pull_request
on: [push, pull_request]

jobs:

Expand All @@ -12,6 +12,8 @@ jobs:

strategy:

fail-fast: false

matrix:

toolchain:
Expand Down Expand Up @@ -63,25 +65,25 @@ jobs:
steps:

- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Create Build Environment
run: cmake -E make_directory ${{runner.workspace}}/build

- name: Configure
working-directory: test
run: cmake -S . -B build ${{ matrix.cmake_opts }}
working-directory: ${{runner.workspace}}/build
shell: bash
run: cmake -DARGPARSE_BUILD_SAMPLES=ON -DCMAKE_BUILD_TYPE=Debug ${{ matrix.cmake_opts }} $GITHUB_WORKSPACE
Comment on lines +74 to +76
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to leave it like the previous implementation, no need to add another step for creating a directory:

Suggested change
working-directory: ${{runner.workspace}}/build
shell: bash
run: cmake -DARGPARSE_BUILD_SAMPLES=ON -DCMAKE_BUILD_TYPE=Debug ${{ matrix.cmake_opts }} $GITHUB_WORKSPACE
run: cmake . -B build -DARGPARSE_BUILD_SAMPLES=ON -DCMAKE_BUILD_TYPE=Debug ${{ matrix.cmake_opts }}

Also setting shell: bash is not needed here.

env:
CC: ${{ matrix.c_compiler }}
CXX: ${{ matrix.cxx_compiler }}

- name: Build for ${{ matrix.os }} with ${{ matrix.compiler }}
working-directory: test
run: cmake --build build
working-directory: ${{runner.workspace}}/build
run: cmake --build . --config Debug --verbose
Comment on lines +82 to +83
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to set the working-directory, we can just use this:

Suggested change
working-directory: ${{runner.workspace}}/build
run: cmake --build . --config Debug --verbose
run: cmake --build build --config Debug --verbose


- name: Test
if: ${{ ! startsWith(matrix.os, 'windows') }}
working-directory: test/build
run: ./tests

- name: Test (Windows)
if: ${{ startsWith(matrix.os, 'windows') }}
working-directory: test/build
run: ./Debug/tests.exe
working-directory: ${{runner.workspace}}/build
run: ctest -C Debug -V
env:
CTEST_OUTPUT_ON_FAILURE: True
Comment on lines +86 to +89
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here, we can also use --test-dir instead of setting the working-directory and use --output-on-failure instead of CTEST_OUTPUT_ON_FAILURE:

Suggested change
working-directory: ${{runner.workspace}}/build
run: ctest -C Debug -V
env:
CTEST_OUTPUT_ON_FAILURE: True
run: ctest --test-dir build -C Debug -V --output-on-failure

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ __pycache__/
# CMake build directory
build

# CMake Testing directory
Testing

# Cppcheck build directory
analysis-cppcheck-build-dir

Expand Down
42 changes: 34 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ project(argparse
LANGUAGES CXX
)

option(ARGPARSE_INSTALL "Include an install target" ON)
option(ARGPARSE_BUILD_TESTS "Build tests" ON)
option(ARGPARSE_INSTALL "Include an install target" ${ARGPARSE_IS_TOP_LEVEL})
option(ARGPARSE_BUILD_TESTS "Build tests" ${ARGPARSE_IS_TOP_LEVEL})
option(ARGPARSE_BUILD_SAMPLES "Build samples" OFF)

include(GNUInstallDirs)
Expand All @@ -28,15 +28,41 @@ target_include_directories(argparse INTERFACE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)


# Pedantic compiler options
# Update if necessary
if(MSVC)
# Force to always compile with W4
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(ARGPARSE_PEDANTIC_COMPILE_FLAGS "/W4")
endif()
Comment on lines +35 to +40
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't just putting /W4 will guarantee it will always replace other lower level warning level?

list(APPEND ARGPARSE_PEDANTIC_COMPILE_FLAGS "/WX")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(ARGPARSE_PEDANTIC_COMPILE_FLAGS -Wall -Wno-long-long -pedantic -Wsign-conversion -Wshadow -Wconversion -Werror -Wextra)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(ARGPARSE_PEDANTIC_COMPILE_FLAGS -Wall -Wno-long-long -pedantic -Wsign-conversion -Wshadow -Wconversion -Werror -Wextra)
Comment on lines +42 to +45
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any differences in here, why don't we use this instead:

Suggested change
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(ARGPARSE_PEDANTIC_COMPILE_FLAGS -Wall -Wno-long-long -pedantic -Wsign-conversion -Wshadow -Wconversion -Werror -Wextra)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(ARGPARSE_PEDANTIC_COMPILE_FLAGS -Wall -Wno-long-long -pedantic -Wsign-conversion -Wshadow -Wconversion -Werror -Wextra)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(ARGPARSE_PEDANTIC_COMPILE_FLAGS -Wall -Wno-long-long -pedantic -Wsign-conversion -Wshadow -Wconversion -Werror -Wextra)

endif()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()

if(ARGPARSE_BUILD_SAMPLES)
add_subdirectory(samples)
endif()

if(ARGPARSE_BUILD_TESTS AND ARGPARSE_IS_TOP_LEVEL)

if(ARGPARSE_BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif()
if(ARGPARSE_INSTALL AND ARGPARSE_IS_TOP_LEVEL)

if(ARGPARSE_INSTALL)
install(TARGETS argparse EXPORT argparseConfig)
install(EXPORT argparseConfig
NAMESPACE argparse::
Expand Down Expand Up @@ -95,6 +121,6 @@ if(ARGPARSE_INSTALL AND ARGPARSE_IS_TOP_LEVEL)
install(FILES "${PKG_CONFIG_FILE_NAME}"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
)
endif()

include(CPack)
include(CPack)
endif()
3 changes: 2 additions & 1 deletion include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1387,8 +1387,9 @@ class Argument {
}
if constexpr (details::IsContainer<T>) {
return any_cast_container<T>(m_values);
} else {
return std::any_cast<T>(m_values.front());
}
return std::any_cast<T>(m_values.front());
}

template <typename T>
Expand Down
35 changes: 7 additions & 28 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
cmake_minimum_required(VERSION 3.6)
project(argparse_samples)

if(MSVC)
# Force to always compile with W4
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
# Update if necessary
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic -Wsign-conversion -Wshadow -Wconversion")
endif()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

# Disable deprecation for windows
if (WIN32)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
endif()

function(add_sample NAME)
ADD_EXECUTABLE(ARGPARSE_SAMPLE_${NAME} ${NAME}.cpp)
INCLUDE_DIRECTORIES("../include" ".")
set_target_properties(ARGPARSE_SAMPLE_${NAME} PROPERTIES OUTPUT_NAME ${NAME})
set_property(TARGET ARGPARSE_SAMPLE_${NAME} PROPERTY CXX_STANDARD 17)
add_executable(argparse_sample_${NAME} ${NAME}.cpp)
target_link_libraries(argparse_sample_${NAME} PRIVATE argparse::argparse)
if(ARGPARSE_PEDANTIC_COMPILE_FLAGS)
target_compile_options(argparse_sample_${NAME} PRIVATE ${ARGPARSE_PEDANTIC_COMPILE_FLAGS})
endif()
set_target_properties(argparse_sample_${NAME} PROPERTIES OUTPUT_NAME ${NAME})
endfunction()

add_sample(positional_argument)
Expand All @@ -43,4 +22,4 @@ add_sample(gathering_remaining_arguments)
add_sample(subcommands)
add_sample(parse_known_args)
add_sample(custom_prefix_characters)
add_sample(custom_assignment_characters)
add_sample(custom_assignment_characters)
2 changes: 1 addition & 1 deletion samples/gathering_remaining_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char *argv[]) {
std::cout << files.size() << " files provided" << std::endl;
for (auto &file : files)
std::cout << file << std::endl;
} catch (std::logic_error &e) {
} catch (const std::logic_error &) {
std::cout << "No files provided" << std::endl;
}
}
39 changes: 7 additions & 32 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
cmake_minimum_required(VERSION 3.6)
project(argparse_tests)

if(MSVC)
# Force to always compile with W4
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
# Update if necessary
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -Wpedantic -Wsign-conversion -Wshadow -Wconversion -Werror -Wextra")
endif()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

# Disable deprecation for windows
if (WIN32)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
endif()

# ARGPARSE executable
# ARGPARSE test executable
file(GLOB ARGPARSE_TEST_SOURCES
main.cpp
test_actions.cpp
Expand Down Expand Up @@ -56,13 +34,10 @@ file(GLOB ARGPARSE_TEST_SOURCES
test_equals_form.cpp
test_prefix_chars.cpp
)
set_source_files_properties(main.cpp
PROPERTIES
COMPILE_DEFINITIONS DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)
ADD_EXECUTABLE(ARGPARSE_TESTS ${ARGPARSE_TEST_SOURCES})
INCLUDE_DIRECTORIES("../include" ".")
set_target_properties(ARGPARSE_TESTS PROPERTIES OUTPUT_NAME tests)
set_property(TARGET ARGPARSE_TESTS PROPERTY CXX_STANDARD 17)

# Set ${PROJECT_NAME} as the startup project
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ARGPARSE_TESTS)
add_executable(argparse_tests ${ARGPARSE_TEST_SOURCES})
if(ARGPARSE_PEDANTIC_COMPILE_FLAGS)
target_compile_options(argparse_tests PRIVATE ${ARGPARSE_PEDANTIC_COMPILE_FLAGS})
endif()
target_link_libraries(argparse_tests PRIVATE argparse::argparse)
add_test(NAME argparse_tests COMMAND argparse_tests)
6 changes: 3 additions & 3 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $ mkdir build
$ cd build
$ cmake ../.
$ make
$ ./tests
$ ./argparse_tests
```

## Windows
Expand All @@ -21,5 +21,5 @@ $ cmake ../. -G "Visual Studio 15 2017"
```

2. Open ARGPARSE.sln
3. Build tests in RELEASE | x64
4. Run tests.exe
3. Build argparse_tests in RELEASE | x64
4. Run argparse_tests.exe
3 changes: 2 additions & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include <doctest.hpp>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.hpp"
2 changes: 1 addition & 1 deletion test/test_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"

using doctest::test_suite;

Expand Down
2 changes: 1 addition & 1 deletion test/test_append.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"

#include <string>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion test/test_as_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"

using doctest::test_suite;

Expand Down
2 changes: 1 addition & 1 deletion test/test_bool_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import argparse;
#include <argparse/argparse.hpp>
#endif

#include <doctest.hpp>
#include "doctest.hpp"

using doctest::test_suite;

Expand Down
2 changes: 1 addition & 1 deletion test/test_choices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import argparse;
#include <argparse/argparse.hpp>
#endif

#include <doctest.hpp>
#include "doctest.hpp"

using doctest::test_suite;

Expand Down
4 changes: 2 additions & 2 deletions test/test_compound_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include <test_utility.hpp>
#include "doctest.hpp"
#include "test_utility.hpp"

using doctest::test_suite;

Expand Down
4 changes: 2 additions & 2 deletions test/test_container_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include <test_utility.hpp>
#include "doctest.hpp"
#include "test_utility.hpp"

using doctest::test_suite;

Expand Down
2 changes: 1 addition & 1 deletion test/test_default_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"

#include <iostream>
#include <sstream>
Expand Down
2 changes: 1 addition & 1 deletion test/test_default_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"
#include <string>

using doctest::test_suite;
Expand Down
2 changes: 1 addition & 1 deletion test/test_equals_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"

#include <iostream>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion test/test_error_reporting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"

#include <iostream>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion test/test_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"

#include <any>

Expand Down
2 changes: 1 addition & 1 deletion test/test_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
#include "doctest.hpp"

#include <optional>
#include <sstream>
Expand Down
Loading
Loading