Skip to content

Commit 372c695

Browse files
Merge pull request #93 from mcharlou/improve-cmake-configuration
Improve cmake configuration
2 parents a5a9d8d + 9565cfe commit 372c695

File tree

2 files changed

+76
-53
lines changed

2 files changed

+76
-53
lines changed

CMakeLists.txt

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ set( CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install"
99

1010
project(bspline-fortran NONE)
1111

12+
# Define build options
13+
option(BSPLINE_BUILD_TESTING "Build the test programs" ON)
14+
option(BSPLINE_BUILD_SHARED_LIBS "Build shared libraries" ON)
15+
option(BSPLINE_BUILD_STATIC_LIBS "Build static libraries" ON)
16+
1217
# Real and Integer kinds
1318
SET(BSPLINE_REAL_KIND "REAL64" CACHE STRING "Real kind parameter")
1419
SET_PROPERTY(CACHE BSPLINE_REAL_KIND PROPERTY STRINGS REAL32 REAL64 REAL128)
@@ -70,56 +75,76 @@ else()
7075
"${ABS_LIB_INSTALL_DIR}" )
7176
endif()
7277

73-
add_library( ${LIB_NAME} SHARED ${SOURCES} )
74-
add_library( ${LIB_NAME}-static STATIC ${SOURCES} )
75-
76-
set_target_properties(${LIB_NAME}-static
77-
PROPERTIES
78-
OUTPUT_NAME "${LIB_NAME}"
79-
if(NOT MSVC_IDE)
80-
PREFIX lib
81-
endif()
82-
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
83-
Fortran_MODULE_DIRECTORY ${MODULE_DIR})
84-
85-
set_target_properties(${LIB_NAME}
86-
PROPERTIES
87-
OUTPUT_NAME "${LIB_NAME}"
88-
if(NOT MSVC_IDE)
89-
PREFIX lib
90-
endif()
91-
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
92-
Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR})
93-
94-
# install library:
95-
install( TARGETS ${LIB_NAME} ${LIB_NAME}-static
96-
LIBRARY DESTINATION "${INSTALL_LIB_DIR}"
97-
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" )
98-
install( DIRECTORY "${MODULE_DIR}/" DESTINATION "${INSTALL_MOD_DIR}" )
78+
if(BSPLINE_BUILD_SHARED_LIBS)
79+
add_library( ${LIB_NAME} SHARED ${SOURCES} )
80+
81+
set_target_properties(${LIB_NAME}
82+
PROPERTIES
83+
OUTPUT_NAME "${LIB_NAME}"
84+
if(NOT MSVC_IDE)
85+
PREFIX lib
86+
endif()
87+
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
88+
Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR})
89+
90+
# install library:
91+
install( TARGETS ${LIB_NAME}
92+
LIBRARY DESTINATION "${INSTALL_LIB_DIR}"
93+
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" )
94+
install( DIRECTORY "${MODULE_DIR}/" DESTINATION "${INSTALL_MOD_DIR}" )
95+
endif()
96+
97+
if(BSPLINE_BUILD_STATIC_LIBS)
98+
add_library( ${LIB_NAME}-static STATIC ${SOURCES} )
99+
100+
set_target_properties(${LIB_NAME}-static
101+
PROPERTIES
102+
OUTPUT_NAME "${LIB_NAME}"
103+
if(NOT MSVC_IDE)
104+
PREFIX lib
105+
endif()
106+
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
107+
Fortran_MODULE_DIRECTORY ${MODULE_DIR})
108+
109+
# install library:
110+
install( TARGETS ${LIB_NAME}-static
111+
LIBRARY DESTINATION "${INSTALL_LIB_DIR}"
112+
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" )
113+
install( DIRECTORY "${MODULE_DIR}/" DESTINATION "${INSTALL_MOD_DIR}" )
114+
endif()
99115

100116
# Windows settings:
101117
if(MSVC_IDE)
102118
INCLUDE_DIRECTORIES("src")
103119
SET(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fpp")
104120
endif()
105121

106-
# Add ctest commands, to build all test programs (that do not depend on pyplot_module)
107-
# Thus, tests can all be run with `make test`
108-
enable_testing()
109-
foreach(prog_name
110-
# bspline_extrap_test
111-
bspline_test_2
112-
bspline_test
113-
# dbint4_test
114-
# knot_tests
115-
# speed_test
116-
# speed_test_oo
117-
stack_size_test_oo
118-
test_integrate
119-
test_oo
120-
test_regrid)
121-
add_executable(${prog_name} test/${prog_name}.f90)
122-
target_link_libraries(${prog_name} ${LIB_NAME}-static)
123-
add_test(NAME ${prog_name}
124-
COMMAND ${prog_name})
125-
endforeach()
122+
if(BSPLINE_BUILD_TESTING)
123+
# Add ctest commands, to build all test programs (that do not depend on pyplot_module)
124+
# Thus, tests can all be run with `make test`
125+
enable_testing()
126+
foreach(prog_name
127+
# bspline_extrap_test
128+
bspline_test_2
129+
bspline_test
130+
# dbint4_test
131+
# knot_tests
132+
# speed_test
133+
# speed_test_oo
134+
stack_size_test_oo
135+
test_integrate
136+
test_oo
137+
test_regrid)
138+
add_executable(${prog_name} test/${prog_name}.f90)
139+
if(BSPLINE_BUILD_STATIC_LIBS)
140+
target_link_libraries(${prog_name} ${LIB_NAME}-static)
141+
target_include_directories(${prog_name} PRIVATE ${MODULE_DIR})
142+
elseif(BSPLINE_BUILD_SHARED_LIBS)
143+
target_link_libraries(${prog_name} ${LIB_NAME})
144+
else()
145+
message(FATAL_ERROR "No libraries to link the test programs to. Either shared or static libraries must be built.")
146+
endif()
147+
add_test(NAME ${prog_name}
148+
COMMAND ${prog_name})
149+
endforeach()
150+
endif()

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,19 @@ bspline-fortran = { git="https://github.com/jacobwilliams/bspline-fortran.git",
155155
A basic CMake configuration file is also included. For example, to build a static library:
156156

157157
```bash
158-
mkdir build
159-
cd build
160-
cmake ..
161-
make
158+
cmake -S. -Bbuild
159+
cmake --build build
162160
```
163161

164-
Or, to build a shared library:
162+
Or, to build a shared library, replace the first CMake command with:
165163

166164
```bash
167-
cmake -DBUILD_SHARED_LIBS=ON ..
165+
cmake -S. -Bbuild -DBUILD_SHARED_LIBS=ON ..
168166
```
169167

170168
For a debug build:
171169
```bash
172-
cmake -DCMAKE_BUILD_TYPE=DEBUG ..
170+
cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=DEBUG ..
173171
```
174172

175173
## Dependencies

0 commit comments

Comments
 (0)