Commit e005d076568ea7a6d1979a8f80a988728e136d5a

Authored by Mario Badr
Committed by Jarryd Beck
1 parent f6bd09df

Use cmake interface library

Fixes #33.

This PR uses cmake's interface library feature:

  An INTERFACE library target does not directly create build output,
  though it may have properties set on it and it may be installed,
  exported and imported.

This makes it easier to include the header only library in a cmake
project. After using add_subdirectory on the cxxopts directory, one
simply needs to include cxxopts in their target_link_libraries, which
will allow the user's target to inherit the properties of the cxxopts
header library (see changes to example and test).
CMakeLists.txt
... ... @@ -26,7 +26,7 @@ RETURN()
26 26  
27 27 ENDIF()
28 28  
29   -cmake_minimum_required(VERSION 2.8.12)
  29 +cmake_minimum_required(VERSION 3.1)
30 30 project(cxxopts)
31 31  
32 32 enable_testing()
... ... @@ -37,9 +37,7 @@ option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON)
37 37 option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" OFF)
38 38  
39 39 set(CXXOPTS_LINKER_LIBRARIES "")
40   -
41 40 set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library")
42   -
43 41 if(CXXOPTS_USE_UNICODE_HELP)
44 42  
45 43 find_package(PkgConfig)
... ... @@ -48,8 +46,23 @@ if(CXXOPTS_USE_UNICODE_HELP)
48 46  
49 47 set(CXXOPTS_LINKER_LIBRARIES "${ICU_LDFLAGS}")
50 48 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE")
51   -
52 49 endif()
53 50  
  51 +add_library(cxxopts INTERFACE)
  52 +target_sources(
  53 + cxxopts INTERFACE
  54 + ${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp
  55 + )
  56 +target_include_directories(
  57 + cxxopts INTERFACE
  58 + include/
  59 + )
  60 +target_link_libraries(
  61 + cxxopts
  62 + INTERFACE ${CXXOPTS_LINKER_LIBRARIES}
  63 + )
  64 +
  65 +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include)
  66 +
54 67 add_subdirectory(src)
55 68 add_subdirectory(test)
... ...
src/cxxopts.hpp renamed to include/cxxopts.hpp
src/CMakeLists.txt
... ... @@ -19,9 +19,8 @@
19 19 # THE SOFTWARE.
20 20  
21 21 if(CXXOPTS_BUILD_EXAMPLES)
22   - add_executable(example example.cpp cxxopts.hpp)
23   -
24   - target_link_libraries(example ${CXXOPTS_LINKER_LIBRARIES})
  22 + add_executable(example example.cpp)
  23 + target_link_libraries(example cxxopts)
25 24  
26 25 if (MSVC)
27 26 target_compile_options(example PUBLIC /W2)
... ... @@ -29,5 +28,3 @@ if(CXXOPTS_BUILD_EXAMPLES)
29 28 target_compile_options(example PUBLIC -std=c++11 -Wall)
30 29 endif()
31 30 endif()
32   -
33   -install(FILES cxxopts.hpp DESTINATION include)
... ...
test/CMakeLists.txt
1 1 if (CXXOPTS_BUILD_TESTS)
2 2 add_executable(options_test options.cpp)
3   -
4   - target_link_libraries(options_test ${CXXOPTS_LINKER_LIBRARIES})
  3 + target_link_libraries(options_test cxxopts)
5 4  
6 5 if (MSVC)
7 6 target_compile_options(options_test PUBLIC /W2)
... ...
test/options.cpp
... ... @@ -3,7 +3,7 @@
3 3  
4 4 #include <initializer_list>
5 5  
6   -#include "../src/cxxopts.hpp"
  6 +#include "cxxopts.hpp"
7 7  
8 8 class Argv {
9 9 public:
... ...