diff --git a/.travis.yml b/.travis.yml index aea97667..4d5902d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: cpp sudo: false - +python: + - "3.6" cache: directories: - ${TRAVIS_BUILD_DIR}/deps/cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 25a4a6ba..e7afcf27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,26 +1,51 @@ -cmake_minimum_required(VERSION 2.8 FATAL_ERROR) +cmake_minimum_required(VERSION 3.1 FATAL_ERROR) project(CLI11 CXX) SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) -# C++11 without GNU extensions -# Requires CMAKE 3.1+ for Mac -if(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} LESS 3.1) - add_compile_options(-std=c++11) -else() - cmake_policy(VERSION 3.1) # Needed for Mac +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + set(CUR_PROJ ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) +else() + set(CUR_PROJ OFF) endif() -# be moderately paranoid with flags + +# Be moderately paranoid with flags +# But only globally, don't inherit add_compile_options(-pedantic -Wall -Wextra) -include_directories(include) -set(headers "${PROJECT_SOURCE_DIR}/include/CLI.hpp") +add_library(CLI INTERFACE) +target_include_directories(CLI INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") -enable_testing() -add_subdirectory(tests) +# Single file test +option(CLI_SINGLE_FILE "Generate a single header file (and test)" ${CUR_PROJ}) +if(CLI_SINGLE_FILE) + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include") + add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" + COMMAND python "${CMAKE_CURRENT_SOURCE_DIR}/scripts/MakeSingleHeader.py" "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" + IMPLICIT_DEPENDS CXX "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" + ) + add_custom_target(generate_cli_single_file + DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp") -add_subdirectory(examples) + add_library(CLI_SINGLE INTERFACE) + target_link_libraries(CLI_SINGLE INTERFACE CLI) + add_dependencies(CLI_SINGLE generate_cli_single_file) + target_compile_definitions(CLI_SINGLE INTERFACE -DCLI_SINGLE_FILE) + target_include_directories(CLI_SINGLE INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/include/") +endif() + +option(CLI_TESTING "Build the tests and add them" ${CUR_PROJ}) +if(CLI_TESTING) + enable_testing() + add_subdirectory(tests) +endif() + +option(CLI_EXAMPLES "Build the examples" ${CUR_PROJ}) +if(CLI_EXAMPLES) + add_subdirectory(examples) +endif() diff --git a/cmake/AddGoogletest.cmake b/cmake/AddGoogletest.cmake index 03a6a8c1..b2552ca7 100644 --- a/cmake/AddGoogletest.cmake +++ b/cmake/AddGoogletest.cmake @@ -32,7 +32,7 @@ include_directories(${gtest_SOURCE_DIR}/include) # Target must already exist macro(add_gtest TESTNAME) - target_link_libraries(${TESTNAME} gtest gtest_main) + target_link_libraries(${TESTNAME} PUBLIC gtest gtest_main) add_test(${TESTNAME} ${TESTNAME}) endmacro() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6ddc1fed..672c2f88 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,4 @@ -add_executable(try try.cpp ${headers}) -add_executable(try1 try1.cpp ${headers}) +add_executable(try try.cpp) +target_link_libraries(try PUBLIC CLI) +add_executable(try1 try1.cpp) +target_link_libraries(try1 PUBLIC CLI) diff --git a/scripts/MakeSingleHeader.py b/scripts/MakeSingleHeader.py index 70965492..a45ba464 100755 --- a/scripts/MakeSingleHeader.py +++ b/scripts/MakeSingleHeader.py @@ -2,35 +2,36 @@ # Requires Python 3.6 -from plumbum import local, cli, FG import re +import argparse +from pathlib import Path includes_local = re.compile(r"""^#include "(.*)"$""", re.MULTILINE) includes_system = re.compile(r"""^#include \<(.*)\>$""", re.MULTILINE) -DIR = local.path(__file__).dirname -BDIR = DIR / '../include' +DIR = Path(__file__).resolve().parent +BDIR = DIR.parent / 'include' -class MakeHeader(cli.Application): +def MakeHeader(out): + main_header = BDIR / 'CLI/CLI.hpp' + with main_header.open() as f: + header = f.read() - def main(self, out : cli.NonexistentPath = BDIR / 'CLI11.hpp'): - main_header = BDIR / 'CLI/CLI.hpp' - header = main_header.read() + include_files = includes_local.findall(header) - include_files = includes_local.findall(header) + headers = set() + output = '' + with open('output.hpp', 'w') as f: + for inc in include_files: + with (BDIR / inc).open() as f: + inner = f.read() + headers |= set(includes_system.findall(inner)) + output += f'\n// From {inc}\n\n' + output += inner[inner.find('namespace'):] - headers = set() - output = '' - with open('output.hpp', 'w') as f: - for inc in include_files: - inner = (BDIR / inc).read() - headers |= set(includes_system.findall(inner)) - output += f'\n// From {inc}\n\n' - output += inner[inner.find('namespace'):] + header_list = '\n'.join(f'#include <{h}>' for h in headers) - header_list = '\n'.join(f'#include <{h}>' for h in headers) - - output = f'''\ + output = f'''\ #pragma once // Distributed under the LGPL version 3.0 license. See accompanying @@ -42,12 +43,14 @@ class MakeHeader(cli.Application): {header_list} {output}''' - with out.open('w') as f: - f.write(output) + with Path(out).open('w') as f: + f.write(output) + + print(f"Created {out}") - print(f"Created {out}") if __name__ == '__main__': - MakeHeader() - - + parser = argparse.ArgumentParser() + parser.add_argument("output", nargs='?', default=BDIR / 'CLI11.hpp') + args = parser.parse_args() + MakeHeader(args.output) diff --git a/tests/CLITest.cpp b/tests/CLITest.cpp index d2d7e766..7516967c 100644 --- a/tests/CLITest.cpp +++ b/tests/CLITest.cpp @@ -1,5 +1,9 @@ - +#ifdef CLI_SINGLE_FILE +#include "CLI11.hpp" +#else #include "CLI/CLI.hpp" +#endif + #include "gtest/gtest.h" #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5bf3d0c2..ef8836d6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,7 +1,21 @@ include(AddGoogletest) -add_executable(CLITest CLITest.cpp ${headers}) +add_executable(CLITest CLITest.cpp) +target_link_libraries(CLITest PUBLIC CLI) add_gtest(CLITest) -add_executable(SmallTest SmallTest.cpp ${headers}) +add_executable(SmallTest SmallTest.cpp) +target_link_libraries(SmallTest PUBLIC CLI) add_gtest(SmallTest) + +if(CLI_SINGLE_FILE) + + add_executable(CLISingleTest CLITest.cpp) + target_link_libraries(CLISingleTest PUBLIC CLI_SINGLE) + add_gtest(CLISingleTest) + + add_executable(SmallSingleTest SmallTest.cpp) + target_link_libraries(SmallSingleTest PUBLIC CLI_SINGLE) + add_gtest(SmallSingleTest) + +endif() diff --git a/tests/SmallTest.cpp b/tests/SmallTest.cpp index a43991fc..a5a2dd16 100644 --- a/tests/SmallTest.cpp +++ b/tests/SmallTest.cpp @@ -1,4 +1,8 @@ +#ifdef CLI_SINGLE_FILE +#include "CLI11.hpp" +#else #include "CLI/CLI.hpp" +#endif #include "gtest/gtest.h" #include #include