Skip to content

Commit 563f2b0

Browse files
authored
Merge pull request #21 from flightaware/BCK-5159
Bck 5159
2 parents 5961faf + e5d9844 commit 563f2b0

File tree

5 files changed

+187
-50
lines changed

5 files changed

+187
-50
lines changed

CMakeLists.txt

Lines changed: 104 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
cmake_minimum_required(VERSION 3.0)
1+
cmake_minimum_required(VERSION 3.8)
22

33
project(cpptcl)
44

5+
set(CPPTCL_VERSION 2.2.5)
6+
57
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
68
message(STATUS "Setting build type to Release as none was specified.")
79
set(CMAKE_BUILD_TYPE Release)
@@ -10,9 +12,16 @@ message(INFO " cmake build type ${CMAKE_BUILD_TYPE}")
1012

1113
set(TCLSH_VERSION_STRING, "8.6")
1214

13-
set(CMAKE_CXX_STANDARD 17)
14-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
15-
set(CMAKE_CXX_EXTENSIONS OFF)
15+
# Determine if cpptcl is built as a subproject (using add_subdirectory)
16+
# or if it is the master project.
17+
set(CPPTCL_MASTER_PROJECT OFF)
18+
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
19+
set(CPPTCL_MASTER_PROJECT ON)
20+
endif()
21+
22+
option(CPPTCL_INSTALL "Generate the install target." ${CPPTCL_MASTER_PROJECT})
23+
option(CPPTCL_TEST "Build the tests." ${CPPTCL_MASTER_PROJECT})
24+
option(CPPTCL_EXAMPLES "Build the examples." ${CPPTCL_MASTER_PROJECT})
1625

1726
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
1827
list(APPEND OPTS -stdlib=libc++)
@@ -47,10 +56,10 @@ if(NOT TCL_LIBRARY)
4756
message(FATAL_ERROR " Tcl library not found")
4857
endif()
4958

50-
include_directories(${cpptcl_SOURCE_DIR} ${cpptcl_SOURCE_DIR}/details ${TCL_INCLUDE_PATH})
51-
5259
add_compile_options(${OPTS})
5360

61+
set(cpptcl_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
62+
5463
list(APPEND SRCS ${cpptcl_SOURCE_DIR}/cpptcl.cc)
5564
list(APPEND HDRS ${cpptcl_SOURCE_DIR}/cpptcl/cpptcl.h)
5665
list(APPEND HDRS ${cpptcl_SOURCE_DIR}/cpptcl/cpptcl_object.h)
@@ -65,41 +74,100 @@ list(APPEND HDRS_DETAILS ${cpptcl_SOURCE_DIR}/cpptcl/details/methods_v.h)
6574
list(APPEND HDRS_DETAILS ${cpptcl_SOURCE_DIR}/cpptcl/details/bind.h)
6675

6776
add_library(cpptcl SHARED ${SRCS} ${HDRS} ${HDRS_DETAILS})
68-
add_library(cpptcl_static STATIC ${SRCS} ${HDRS} ${HDRS_DETAILS})
69-
set_property(TARGET cpptcl_static PROPERTY POSITION_INDEPENDENT_CODE ON)
77+
add_library(cpptcl::cpptcl ALIAS cpptcl)
78+
target_compile_features(cpptcl PUBLIC cxx_std_11)
79+
set_target_properties(cpptcl PROPERTIES
80+
CXX_EXTENSIONS OFF
81+
CXX_STANDARD_REQUIRED ON)
82+
target_include_directories(cpptcl
83+
PUBLIC
84+
$<BUILD_INTERFACE:${cpptcl_INCLUDE_DIR}>
85+
$<INSTALL_INTERFACE:include>
86+
)
87+
target_include_directories(cpptcl PUBLIC ${TCL_INCLUDE_PATH})
7088
target_link_libraries(cpptcl ${TCL_STUB_LIBRARY})
7189

90+
add_library(cpptcl_static STATIC ${SRCS} ${HDRS} ${HDRS_DETAILS})
91+
add_library(cpptcl::cpptcl_static ALIAS cpptcl_static)
92+
target_compile_features(cpptcl_static PUBLIC cxx_std_11)
93+
set_target_properties(cpptcl_static PROPERTIES
94+
POSITION_INDEPENDENT_CODE ON
95+
CXX_EXTENSIONS OFF
96+
CXX_STANDARD_REQUIRED ON)
97+
target_include_directories(cpptcl_static
98+
PUBLIC
99+
$<BUILD_INTERFACE:${cpptcl_INCLUDE_DIR}>
100+
$<INSTALL_INTERFACE:include>
101+
)
102+
target_include_directories(cpptcl_static PUBLIC ${TCL_INCLUDE_PATH})
103+
72104
add_library(cpptcl_runtime STATIC ${cpptcl_SOURCE_DIR}/cpptcl_runtime.c)
73-
set_property(TARGET cpptcl_runtime PROPERTY POSITION_INDEPENDENT_CODE ON)
105+
add_library(cpptcl::cpptcl_runtime ALIAS cpptcl_runtime)
106+
target_compile_features(cpptcl_runtime PUBLIC cxx_std_11)
107+
set_target_properties(cpptcl_runtime PROPERTIES
108+
POSITION_INDEPENDENT_CODE ON
109+
CXX_EXTENSIONS OFF
110+
CXX_STANDARD_REQUIRED ON)
111+
target_include_directories(cpptcl_runtime
112+
PUBLIC
113+
$<BUILD_INTERFACE:${cpptcl_INCLUDE_DIR}>
114+
$<INSTALL_INTERFACE:include>
115+
)
116+
target_include_directories(cpptcl_runtime PUBLIC ${TCL_INCLUDE_PATH})
74117
target_link_libraries(cpptcl_runtime ${TCL_LIBRARY})
75118

76-
install(TARGETS cpptcl LIBRARY DESTINATION lib)
77-
install(TARGETS cpptcl_static ARCHIVE DESTINATION lib)
78-
install(TARGETS cpptcl_runtime ARCHIVE DESTINATION lib)
79-
install(FILES ${HDRS} DESTINATION include/cpptcl)
80-
install(FILES ${HDRS_DETAILS} DESTINATION include/cpptcl/details)
81-
82-
add_subdirectory(test EXCLUDE_FROM_ALL)
83-
add_subdirectory(examples EXCLUDE_FROM_ALL)
84-
85-
list(APPEND LIST_OF_TESTS test1)
86-
list(APPEND LIST_OF_TESTS test2)
87-
list(APPEND LIST_OF_TESTS test3)
88-
list(APPEND LIST_OF_TESTS test4)
89-
list(APPEND LIST_OF_TESTS test5)
90-
list(APPEND LIST_OF_TESTS test6)
91-
list(APPEND LIST_OF_TESTS test7)
92-
list(APPEND LIST_OF_TESTS test_main)
93-
94-
add_custom_target(tests DEPENDS ${LIST_OF_TESTS})
119+
if (CPPTCL_TEST)
120+
add_subdirectory(test)
121+
endif()
95122

96-
list(APPEND LIST_OF_EXAMPLES example2)
97-
list(APPEND LIST_OF_EXAMPLES example6)
98-
list(APPEND LIST_OF_EXAMPLES cpptcl_module_two)
99-
list(APPEND LIST_OF_EXAMPLES cpptcl_module_three)
100-
list(APPEND LIST_OF_EXAMPLES cpptcl_module_five)
101-
list(APPEND LIST_OF_EXAMPLES cpptcl_module_six)
102-
list(APPEND LIST_OF_EXAMPLES cpptcl_example_functions)
123+
if (CPPTCL_EXAMPLES)
124+
add_subdirectory(examples)
125+
endif()
103126

104-
add_custom_target(examples DEPENDS ${LIST_OF_EXAMPLES})
127+
if (CPPTCL_INSTALL)
128+
include(GNUInstallDirs)
129+
include(CMakePackageConfigHelpers)
130+
131+
set(cpptcl_INCLUDE_DIR include/ CACHE STRING
132+
"cpptcl header install directory")
133+
set(CPPTCL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/cpptcl CACHE STRING
134+
"Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.")
135+
set(CPPTCL_VERSION_CONFIG ${PROJECT_BINARY_DIR}/cpptcl-config-version.cmake)
136+
set(CPPTCL_PROJECT_CONFIG ${PROJECT_BINARY_DIR}/cpptcl-config.cmake)
137+
138+
configure_package_config_file(
139+
"cmake/cpptcl-config.cmake.in"
140+
${CPPTCL_PROJECT_CONFIG}
141+
INSTALL_DESTINATION ${CPPTCL_CMAKE_DIR}
142+
PATH_VARS cpptcl_INCLUDE_DIR)
143+
144+
write_basic_package_version_file(
145+
${CPPTCL_VERSION_CONFIG}
146+
VERSION ${CPPTCL_VERSION}
147+
COMPATIBILITY SameMajorVersion )
148+
149+
install(
150+
FILES ${CPPTCL_PROJECT_CONFIG} ${CPPTCL_VERSION_CONFIG}
151+
DESTINATION ${CPPTCL_CMAKE_DIR})
152+
153+
list(APPEND CPPTCL_INSTALL_TARGETS cpptcl)
154+
list(APPEND CPPTCL_INSTALL_TARGETS cpptcl_static)
155+
list(APPEND CPPTCL_INSTALL_TARGETS cpptcl_runtime)
156+
157+
install(
158+
TARGETS ${CPPTCL_INSTALL_TARGETS}
159+
EXPORT CPPTCL_ALL_TARGETS
160+
LIBRARY DESTINATION lib
161+
ARCHIVE DESTINATION lib)
162+
install(
163+
DIRECTORY cpptcl/
164+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cpptcl
165+
FILES_MATCHING PATTERN *.h)
166+
167+
install(
168+
EXPORT CPPTCL_ALL_TARGETS
169+
NAMESPACE cpptcl::
170+
FILE cpptcl-targets.cmake
171+
DESTINATION ${CPPTCL_CMAKE_DIR} )
105172

173+
endif()

COMMONmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ install: all
1515
(cd build; make install)
1616

1717
test: build/Makefile
18-
(cd build; make tests; ctest)
18+
(cd build; ctest)
1919

2020
examples: build/Makefile
2121
(cd build; make examples)

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ The C++/Tcl itself consists of these files:
4242

4343
We're using cmake to generate makefiles. This is pretty standard in the C++ world.
4444

45-
In order to compile the tests and the examples you may need to change the compiler options to fit your particular environment (different paths to the Tcl headers and libs, another compiler, etc.). By default tests and examples are not built, but you can build them with the test and examples make targets respectfully.
45+
In order to compile the tests and the examples you may need to change the compiler options to fit your particular environment (different paths to the Tcl headers and libs, another compiler, etc.). Tests and examples are built by default when building cpptcl (although not when included as a cmake subproject). You can disable the tests and examples by setting `-DCPPTCL_TEST=OFF` and `-DCPPTCL_EXAMPLES=OFF` respectively.
46+
47+
---
48+
49+
cmake
50+
51+
The cpptcl install includes a cpptcl-config.cmake and target files for easy inclusion using cmake. You can therefore use `find_package(cpptcl)` to add the dependency. It will expose the `cpptcl_INCLUDE_DIR` variable with the header file location and the following targets to link against:
52+
53+
cpptcl::cpptcl
54+
cpptcl::cpptcl_static
55+
cpptcl::cpptcl_runtime
56+
57+
Here's a simple example of including cpptcl in your CMakeLists.txt file:
58+
59+
```
60+
find_package(cpptcl REQUIRED)
61+
find_package(TCL REQUIRED)
62+
63+
add_executable(myexec main.cpp)
64+
target_include_directories(myexec PRIVATE ${cpptcl_INCLUDE_DIR})
65+
target_include_directories(myexec PRIVATE ${TCL_INCLUDE_PATH})
66+
target_link_libraries(myexec cpptcl::cpptcl)
67+
target_link_libraries(myexec ${TCL_LIBRARY})
68+
```
4669

4770
Anyway - have fun! :-)

cmake/cpptcl-config.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
3+
set_and_check(cpptcl_INCLUDE_DIR "@PACKAGE_cpptcl_INCLUDE_DIR@")
4+
5+
include(${CMAKE_CURRENT_LIST_DIR}/cpptcl-targets.cmake)

test/CMakeLists.txt

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,76 @@
11
cmake_minimum_required(VERSION 3.0)
22

3+
set(TCL_TCLSH tclsh8.6)
4+
35
add_executable(test1 test1.cc ../cpptcl.cc)
46
add_test(test1 test1)
5-
target_link_libraries(test1 PUBLIC ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
7+
target_compile_features(test1 PUBLIC cxx_std_11)
8+
set_target_properties(test1 PROPERTIES
9+
CXX_EXTENSIONS OFF
10+
CXX_STANDARD_REQUIRED ON)
11+
target_include_directories(test1 PRIVATE ${cpptcl_INCLUDE_DIR} ${TCL_INCLUDE_PATH})
12+
target_link_libraries(test1 PRIVATE ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
13+
14+
add_library(test2 SHARED test2.cc ../cpptcl.cc)
15+
add_test(NAME test2 COMMAND ${TCL_TCLSH} ${CMAKE_SOURCE_DIR}/test/test2.tcl)
16+
target_compile_features(test2 PUBLIC cxx_std_11)
17+
set_target_properties(test2 PROPERTIES
18+
CXX_EXTENSIONS OFF
19+
CXX_STANDARD_REQUIRED ON)
20+
target_include_directories(test2 PRIVATE ${cpptcl_INCLUDE_DIR} ${TCL_INCLUDE_PATH})
21+
target_link_libraries(test2 PRIVATE ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
622

723
add_executable(test3 test3.cc ../cpptcl.cc)
824
add_test(test3 test3)
9-
target_link_libraries(test3 PUBLIC ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
25+
target_compile_features(test3 PUBLIC cxx_std_11)
26+
set_target_properties(test3 PROPERTIES
27+
CXX_EXTENSIONS OFF
28+
CXX_STANDARD_REQUIRED ON)
29+
target_include_directories(test3 PRIVATE ${cpptcl_INCLUDE_DIR} ${TCL_INCLUDE_PATH})
30+
target_link_libraries(test3 PRIVATE ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
1031

1132
add_executable(test4 test4.cc ../cpptcl.cc)
1233
add_test(test4 test4)
13-
target_link_libraries(test4 PUBLIC ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
34+
target_compile_features(test4 PUBLIC cxx_std_11)
35+
set_target_properties(test4 PROPERTIES
36+
CXX_EXTENSIONS OFF
37+
CXX_STANDARD_REQUIRED ON)
38+
target_include_directories(test4 PRIVATE ${cpptcl_INCLUDE_DIR} ${TCL_INCLUDE_PATH})
39+
target_link_libraries(test4 PRIVATE ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
1440

1541
add_executable(test5 test5.cc ../cpptcl.cc)
1642
add_test(test5 test5)
17-
target_link_libraries(test5 PUBLIC ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
43+
target_compile_features(test5 PUBLIC cxx_std_11)
44+
set_target_properties(test5 PROPERTIES
45+
CXX_EXTENSIONS OFF
46+
CXX_STANDARD_REQUIRED ON)
47+
target_include_directories(test5 PRIVATE ${cpptcl_INCLUDE_DIR} ${TCL_INCLUDE_PATH})
48+
target_link_libraries(test5 PRIVATE ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
1849

1950
add_executable(test6 test6.cc ../cpptcl.cc)
2051
add_test(test6 test6)
21-
target_link_libraries(test6 PUBLIC ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
52+
target_compile_features(test6 PUBLIC cxx_std_11)
53+
set_target_properties(test6 PROPERTIES
54+
CXX_EXTENSIONS OFF
55+
CXX_STANDARD_REQUIRED ON)
56+
target_include_directories(test6 PRIVATE ${cpptcl_INCLUDE_DIR} ${TCL_INCLUDE_PATH})
57+
target_link_libraries(test6 PRIVATE ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
2258

2359
add_executable(test7 test7.cc ../cpptcl.cc)
2460
add_test(test7 test7)
25-
target_link_libraries(test7 PUBLIC ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
61+
target_compile_features(test7 PUBLIC cxx_std_11)
62+
set_target_properties(test7 PROPERTIES
63+
CXX_EXTENSIONS OFF
64+
CXX_STANDARD_REQUIRED ON)
65+
target_include_directories(test7 PRIVATE ${cpptcl_INCLUDE_DIR} ${TCL_INCLUDE_PATH})
66+
target_link_libraries(test7 PRIVATE ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
2667

2768
add_executable(test_main test_main.cc ../cpptcl.cc)
2869
add_test(test_main test_main)
29-
target_link_libraries(test_main PUBLIC ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
30-
31-
set(TCL_TCLSH tclsh8.6)
32-
add_library(test2 SHARED test2.cc ../cpptcl.cc)
33-
target_link_libraries(test2 PUBLIC ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
34-
add_test(NAME test2 COMMAND ${TCL_TCLSH} ${CMAKE_SOURCE_DIR}/test/test2.tcl)
70+
target_compile_features(test_main PUBLIC cxx_std_11)
71+
set_target_properties(test_main PROPERTIES
72+
CXX_EXTENSIONS OFF
73+
CXX_STANDARD_REQUIRED ON)
74+
target_include_directories(test_main PRIVATE ${cpptcl_INCLUDE_DIR} ${TCL_INCLUDE_PATH})
75+
target_link_libraries(test_main PRIVATE ${TCL_LIBRARY} ${TCL_STUB_LIBRARY})
3576

0 commit comments

Comments
 (0)