Skip to content

Commit 16abe03

Browse files
authored
Allow project to be used as a library
See cpm-cmake/CPM.cmake#275 (comment)
1 parent 2ccebdc commit 16abe03

File tree

1 file changed

+70
-64
lines changed

1 file changed

+70
-64
lines changed

CMakeLists.txt

+70-64
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,32 @@ add_library(fpm INTERFACE)
88

99
target_include_directories(fpm INTERFACE include)
1010

11+
# if this project is being used as a submodule, it's someone elses' dependency.
12+
# in that case, we don't want to set up any testing, since they just want to
13+
# use it as a library.
14+
get_directory_property(hasParent PARENT_DIRECTORY)
15+
if(NOT hasParent)
1116
#
1217
# Test suite
1318
#
1419
enable_testing()
1520
include(GoogleTest)
1621

1722
add_executable(fpm-test
18-
tests/arithmetic.cpp
19-
tests/arithmetic_int.cpp
20-
tests/basic_math.cpp
21-
tests/constants.cpp
22-
tests/conversion.cpp
23-
tests/classification.cpp
24-
tests/customizations.cpp
25-
tests/detail.cpp
26-
tests/input.cpp
27-
tests/manip.cpp
28-
tests/nearest.cpp
29-
tests/output.cpp
30-
tests/power.cpp
31-
tests/trigonometry.cpp
23+
tests/arithmetic.cpp
24+
tests/arithmetic_int.cpp
25+
tests/basic_math.cpp
26+
tests/constants.cpp
27+
tests/conversion.cpp
28+
tests/classification.cpp
29+
tests/customizations.cpp
30+
tests/detail.cpp
31+
tests/input.cpp
32+
tests/manip.cpp
33+
tests/nearest.cpp
34+
tests/output.cpp
35+
tests/power.cpp
36+
tests/trigonometry.cpp
3237
)
3338
set_target_properties(fpm-test PROPERTIES CXX_STANDARD 11)
3439
target_link_libraries(fpm-test PRIVATE fpm gtest_main)
@@ -44,29 +49,29 @@ include(ExternalProject)
4449
set(LIBFIXMATH_ROOT "${CMAKE_CURRENT_BINARY_DIR}/libfixmath")
4550

4651
ExternalProject_Add(libfixmath-external
47-
GIT_REPOSITORY "https://github.com/PetteriAimonen/libfixmath.git"
48-
GIT_TAG bada934981a5961569ad2cb6b9006ed94542cc9c
49-
PREFIX ${LIBFIXMATH_ROOT}
50-
CONFIGURE_COMMAND ""
51-
BUILD_COMMAND ""
52-
INSTALL_COMMAND ""
52+
GIT_REPOSITORY "https://github.com/PetteriAimonen/libfixmath.git"
53+
GIT_TAG bada934981a5961569ad2cb6b9006ed94542cc9c
54+
PREFIX ${LIBFIXMATH_ROOT}
55+
CONFIGURE_COMMAND ""
56+
BUILD_COMMAND ""
57+
INSTALL_COMMAND ""
5358
)
5459

5560
set(LIBFIXMATH_SOURCES
56-
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16.c
57-
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16_exp.c
58-
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16_sqrt.c
59-
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16_str.c
60-
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16_trig.c
61-
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fract32.c
62-
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/uint32.c
61+
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16.c
62+
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16_exp.c
63+
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16_sqrt.c
64+
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16_str.c
65+
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fix16_trig.c
66+
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/fract32.c
67+
${LIBFIXMATH_ROOT}/src/libfixmath-external/libfixmath/uint32.c
6368
)
6469
set_source_files_properties(${LIBFIXMATH_SOURCES} PROPERTIES GENERATED TRUE)
6570
add_library(libfixmath ${LIBFIXMATH_SOURCES})
6671

6772
# Set libfixmath compile options to match fpm functionality
6873
target_compile_definitions(libfixmath
69-
PUBLIC
74+
PUBLIC
7075
FIXMATH_NO_CACHE # No caching of results
7176

7277
#FIXMATH_NO_OVERFLOW # We want no runtime-overflow detection, but it fails to compile
@@ -86,12 +91,12 @@ add_dependencies(libfixmath libfixmath-external)
8691
set(LIBCNL_ROOT "${CMAKE_CURRENT_BINARY_DIR}/libcnl")
8792

8893
ExternalProject_Add(libcnl-external
89-
GIT_REPOSITORY "https://github.com/johnmcfarlane/cnl"
90-
GIT_TAG 4d445566fe7c6c8939fffc145a2f30fd587796a6
91-
PREFIX ${LIBCNL_ROOT}
92-
CONFIGURE_COMMAND ""
93-
BUILD_COMMAND ""
94-
INSTALL_COMMAND ""
94+
GIT_REPOSITORY "https://github.com/johnmcfarlane/cnl"
95+
GIT_TAG 4d445566fe7c6c8939fffc145a2f30fd587796a6
96+
PREFIX ${LIBCNL_ROOT}
97+
CONFIGURE_COMMAND ""
98+
BUILD_COMMAND ""
99+
INSTALL_COMMAND ""
95100
)
96101

97102
add_library(libcnl INTERFACE)
@@ -103,9 +108,9 @@ add_dependencies(libcnl libcnl-external)
103108
# Runs benchmarks of FPM operations and dumps results to standard output.
104109
#
105110
add_executable(fpm-benchmark
106-
benchmarks/arithmetic.cpp
107-
benchmarks/power.cpp
108-
benchmarks/trigonometry.cpp
111+
benchmarks/arithmetic.cpp
112+
benchmarks/power.cpp
113+
benchmarks/trigonometry.cpp
109114
)
110115
target_link_libraries(fpm-benchmark PRIVATE fpm libfixmath libcnl benchmark benchmark_main)
111116

@@ -114,20 +119,20 @@ target_link_libraries(fpm-benchmark PRIVATE fpm libfixmath libcnl benchmark benc
114119
# Dumps the accuracy of approximated FPM functions to JSON files.
115120
#
116121
add_executable(fpm-accuracy
117-
accuracy/accuracy.cpp
122+
accuracy/accuracy.cpp
118123
)
119124
set_target_properties(fpm-accuracy PROPERTIES CXX_STANDARD 14)
120125
target_link_libraries(fpm-accuracy PRIVATE fpm libfixmath)
121126

122127

123128
include(FindGnuplot)
124129
if (GNUPLOT_FOUND)
125-
# Create accuracy data
126-
set(DATA_DIR ${CMAKE_CURRENT_BINARY_DIR})
130+
# Create accuracy data
131+
set(DATA_DIR ${CMAKE_CURRENT_BINARY_DIR})
127132

128-
set(DATA_FILES_ACCURACY "")
129-
set(IMG_FILES_ACCURACY "")
130-
foreach(DATA sin-trig cos-trig tan-trig asin-invtrig acos-invtrig atan-invtrig atan2-trig sqrt-auto cbrt-auto pow-auto exp-auto exp2-auto log-auto log2-auto log10-auto)
133+
set(DATA_FILES_ACCURACY "")
134+
set(IMG_FILES_ACCURACY "")
135+
foreach(DATA sin-trig cos-trig tan-trig asin-invtrig acos-invtrig atan-invtrig atan2-trig sqrt-auto cbrt-auto pow-auto exp-auto exp2-auto log-auto log2-auto log10-auto)
131136
string(REGEX MATCHALL "[^-]+" M ${DATA})
132137
list(GET M 0 SERIES)
133138
list(GET M 1 TYPE)
@@ -137,62 +142,63 @@ if (GNUPLOT_FOUND)
137142
list(APPEND IMG_FILES_ACCURACY ${IMG_FILE})
138143

139144
add_custom_command(
140-
OUTPUT ${IMG_FILE}
141-
COMMAND ${GNUPLOT_EXECUTABLE} -c ${PROJECT_SOURCE_DIR}/accuracy/accuracy.gnuplot ${SERIES} ${TYPE}
142-
DEPENDS ${DATA_DIR}/${SERIES}.csv accuracy/accuracy.gnuplot
143-
WORKING_DIRECTORY ${DATA_DIR}
144-
VERBATIM
145-
COMMENT "Plotting ${SERIES} accuracy data"
145+
OUTPUT ${IMG_FILE}
146+
COMMAND ${GNUPLOT_EXECUTABLE} -c ${PROJECT_SOURCE_DIR}/accuracy/accuracy.gnuplot ${SERIES} ${TYPE}
147+
DEPENDS ${DATA_DIR}/${SERIES}.csv accuracy/accuracy.gnuplot
148+
WORKING_DIRECTORY ${DATA_DIR}
149+
VERBATIM
150+
COMMENT "Plotting ${SERIES} accuracy data"
146151
)
147-
endforeach(DATA)
152+
endforeach(DATA)
148153

149-
add_custom_command(
154+
add_custom_command(
150155
OUTPUT ${DATA_FILES_ACCURACY}
151156
COMMAND fpm-accuracy
152157
DEPENDS fpm-accuracy
153158
WORKING_DIRECTORY ${DATA_DIR}
154159
VERBATIM
155160
COMMENT "Generating accuracy data with fpm-accuracy"
156-
)
161+
)
157162

158-
add_custom_target(fpm-accuracy-images DEPENDS ${IMG_FILES_ACCURACY})
163+
add_custom_target(fpm-accuracy-images DEPENDS ${IMG_FILES_ACCURACY})
159164
endif()
160165

161166
if (CMAKE_BUILD_TYPE STREQUAL "Release")
162-
# Enable the performance data-generation command only for Release builds.
167+
# Enable the performance data-generation command only for Release builds.
163168

164-
set(DATA_FILE_PERFORMANCE_JSON ${DATA_DIR}/performance.json)
165-
add_custom_command(
169+
set(DATA_FILE_PERFORMANCE_JSON ${DATA_DIR}/performance.json)
170+
add_custom_command(
166171
OUTPUT ${DATA_FILE_PERFORMANCE_JSON}
167172
COMMAND fpm-benchmark --benchmark_out=${DATA_FILE_PERFORMANCE_JSON} --benchmark_out_format=json
168173
DEPENDS fpm-benchmark
169174
WORKING_DIRECTORY ${DATA_DIR}
170175
VERBATIM
171176
COMMENT "Generating performance data with fpm-benchmark"
172-
)
177+
)
173178

174-
set(DATA_FILE_PERFORMANCE_GNUPLOT ${DATA_DIR}/performance.csv)
175-
add_custom_command(
179+
set(DATA_FILE_PERFORMANCE_GNUPLOT ${DATA_DIR}/performance.csv)
180+
add_custom_command(
176181
OUTPUT ${DATA_FILE_PERFORMANCE_GNUPLOT}
177182
COMMAND python ${PROJECT_SOURCE_DIR}/benchmarks/benchmark.py ${DATA_FILE_PERFORMANCE_JSON} ${DATA_FILE_PERFORMANCE_GNUPLOT}
178183
DEPENDS ${DATA_FILE_PERFORMANCE_JSON} ${PROJECT_SOURCE_DIR}/benchmarks/benchmark.py
179184
WORKING_DIRECTORY ${DATA_DIR}
180185
VERBATIM
181186
COMMENT "Converting performance data for GnuPlot"
182-
)
187+
)
183188

184-
set(IMG_FILE_PERFORMANCE "{$DATA_DIR}/performance.png")
185-
add_custom_command(
189+
set(IMG_FILE_PERFORMANCE "{$DATA_DIR}/performance.png")
190+
add_custom_command(
186191
OUTPUT ${IMG_FILE_PERFORMANCE}
187192
COMMAND ${GNUPLOT_EXECUTABLE} ${PROJECT_SOURCE_DIR}/benchmarks/benchmark.gnuplot
188193
DEPENDS ${DATA_FILE_PERFORMANCE_GNUPLOT} ${PROJECT_SOURCE_DIR}/benchmarks/benchmark.gnuplot
189194
WORKING_DIRECTORY ${DATA_DIR}
190195
VERBATIM
191196
COMMENT "Plotting performance data"
192-
)
197+
)
193198

194-
add_custom_target(fpm-performance-images DEPENDS ${IMG_FILE_PERFORMANCE})
199+
add_custom_target(fpm-performance-images DEPENDS ${IMG_FILE_PERFORMANCE})
195200
endif()
196201

197202
add_subdirectory(3rdparty/googlebench)
198203
add_subdirectory(3rdparty/googletest)
204+
endif()

0 commit comments

Comments
 (0)