提交 d5c0d06e 编写于 作者: J Julien Schueller

CMake: Add proper header generation rules

上级 454c6678
......@@ -193,12 +193,20 @@ endif ()
# nlopt LIBRARY TARGET (SHARED OR STATIC)
#==============================================================================
configure_file (src/api/nlopt.h ${PROJECT_BINARY_DIR}/src/api/nlopt.h COPYONLY)
set (NLOPT_HEADERS
${PROJECT_BINARY_DIR}/src/api/nlopt.h ${PROJECT_BINARY_DIR}/src/api/nlopt.hpp ${PROJECT_BINARY_DIR}/src/api/nlopt.f
src/api/nlopt.h ${PROJECT_BINARY_DIR}/nlopt.hpp ${PROJECT_BINARY_DIR}/nlopt.f
)
add_custom_command (OUTPUT nlopt.hpp
COMMAND ${CMAKE_COMMAND} -DAPI_SOURCE_DIR=${PROJECT_SOURCE_DIR}/src/api -P ${PROJECT_SOURCE_DIR}/cmake/generate-cpp.cmake
MAIN_DEPENDENCY src/api/nlopt-in.hpp)
add_custom_target (generate-cpp ALL DEPENDS nlopt.hpp)
add_custom_command (OUTPUT nlopt.f
COMMAND ${CMAKE_COMMAND} -DAPI_SOURCE_DIR=${PROJECT_SOURCE_DIR}/src/api -P ${PROJECT_SOURCE_DIR}/cmake/generate-fortran.cmake
MAIN_DEPENDENCY src/api/nlopt.h)
add_custom_target (generate-fortran ALL DEPENDS nlopt.f)
set (NLOPT_SOURCES
src/algs/direct/DIRect.c src/algs/direct/direct_wrap.c src/algs/direct/DIRserial.c src/algs/direct/DIRsubrout.c src/algs/direct/direct-internal.h src/algs/direct/direct.h
src/algs/cdirect/cdirect.c src/algs/cdirect/hybrid.c src/algs/cdirect/cdirect.h
......@@ -215,7 +223,7 @@ set (NLOPT_SOURCES
src/algs/isres/isres.c src/algs/isres/isres.h
src/algs/slsqp/slsqp.c src/algs/slsqp/slsqp.h
src/algs/esch/esch.c src/algs/esch/esch.h
src/api/general.c src/api/options.c src/api/optimize.c src/api/deprecated.c src/api/nlopt-internal.h src/api/nlopt.h src/api/f77api.c src/api/f77funcs.h src/api/f77funcs_.h src/api/nlopt.hpp src/api/nlopt-in.hpp
src/api/general.c src/api/options.c src/api/optimize.c src/api/deprecated.c src/api/nlopt-internal.h src/api/nlopt.h src/api/f77api.c src/api/f77funcs.h src/api/f77funcs_.h ${PROJECT_BINARY_DIR}/nlopt.hpp
src/util/mt19937ar.c src/util/sobolseq.c src/util/soboldata.h src/util/timer.c src/util/stop.c src/util/nlopt-util.h src/util/redblack.c src/util/redblack.h src/util/qsort_r.c src/util/rescale.c
)
......@@ -232,6 +240,7 @@ install (FILES ${NLOPT_HEADERS} DESTINATION ${RELATIVE_INSTALL_INCLUDE_DIR})
set (nlopt_lib nlopt)
add_library (${nlopt_lib} ${NLOPT_SOURCES})
add_dependencies(${nlopt_lib} generate-cpp)
target_link_libraries (${nlopt_lib} ${M_LIBRARY})
set_target_properties (${nlopt_lib} PROPERTIES SOVERSION ${SO_MAJOR})
......
cmake_minimum_required (VERSION 2.8.11)
# generate nlopt.hpp from nlopt-in.hpp
file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "")
file (STRINGS ${API_SOURCE_DIR}/nlopt-in.hpp NLOPT_HPP_LINES)
foreach (NLOPT_HPP_LINE ${NLOPT_HPP_LINES})
list(LENGTH NLOPT_HPP_LINE line_len)
# handling trailing backlashes in "file (STRINGS" is a little tricky
if (line_len VERSION_LESS 8)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${NLOPT_HPP_LINE}\n")
else ()
set (prev_inst FALSE)
foreach(NLOPT_HPP_SUBLINE ${NLOPT_HPP_LINE})
# test is we need to add the eaten semicolon
if (NLOPT_HPP_SUBLINE MATCHES "\\)$" OR NLOPT_HPP_SUBLINE MATCHES "return")
set (new_inst TRUE)
else ()
set (new_inst FALSE)
endif ()
if (NOT prev_inst)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${NLOPT_HPP_SUBLINE}")
if (new_inst)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp ";")
endif ()
list (FIND NLOPT_HPP_LINE "${NLOPT_HPP_SUBLINE}" index)
math (EXPR index "${index} + 1")
list (LENGTH NLOPT_HPP_LINE total)
if (NOT index STREQUAL total)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " \\")
endif ()
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "\n")
endif ()
set (prev_inst ${new_inst})
endforeach ()
endif ()
if (NLOPT_HPP_LINE MATCHES "GEN_ENUMS_HERE")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " enum algorithm {\n")
file (STRINGS ${API_SOURCE_DIR}/nlopt.h NLOPT_H_LINES REGEX " NLOPT_[A-Z0-9_]+")
foreach (NLOPT_H_LINE ${NLOPT_H_LINES})
string (REGEX REPLACE "NLOPT_" "" ENUM_LINE ${NLOPT_H_LINE})
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${ENUM_LINE}\n")
if (NLOPT_H_LINE MATCHES "NLOPT_NUM_ALGORITHMS")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " };\n enum result {\n")
elseif (NLOPT_H_LINE MATCHES "NLOPT_NUM_RESULTS")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " };\n")
endif ()
endforeach ()
endif ()
endforeach ()
cmake_minimum_required (VERSION 2.8.11)
# generate nlopt.f from nlopt.h enums
file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/nlopt.f "")
file (STRINGS ${API_SOURCE_DIR}/nlopt.h NLOPT_H_LINES REGEX " NLOPT_[A-Z0-9_]+")
set (i 0)
foreach (NLOPT_H_LINE ${NLOPT_H_LINES})
if (NOT NLOPT_H_LINE MATCHES "NLOPT_NUM_")
string (REGEX REPLACE ".*NLOPT_([A-Z0-9_]+).*" "\\1" ENUM_STRING ${NLOPT_H_LINE})
string (REGEX REPLACE ".*NLOPT_[A-Z0-9_]+ = (-?[0-9]+).*" "\\1" ENUM_VAL ${NLOPT_H_LINE})
if (ENUM_VAL MATCHES "^-?[0-9]+$")
set (i ${ENUM_VAL})
endif ()
set (ENUM_LINE " integer NLOPT_${ENUM_STRING}\n parameter (NLOPT_${ENUM_STRING}=${i})\n")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.f "${ENUM_LINE}")
# https://public.kitware.com/Bug/print_bug_page.php?bug_id=8996
if (i MATCHES "^-")
math (EXPR i "1 ${i}")
else ()
math (EXPR i "${i} + 1")
endif ()
endif ()
endforeach ()
......@@ -3,78 +3,3 @@
if (UNIX)
install (FILES nlopt.3 nlopt_minimize.3 nlopt_minimize_constrained.3 DESTINATION ${INSTALL_MAN_DIR}/man3)
endif ()
# generate nlopt.f from nlopt.h enums
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/nlopt.f)
file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/nlopt.f "")
file (STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/nlopt.h NLOPT_H_LINES REGEX " NLOPT_[A-Z0-9_]+")
set (i 0)
foreach (NLOPT_H_LINE ${NLOPT_H_LINES})
if (NOT NLOPT_H_LINE MATCHES "NLOPT_NUM_")
string (REGEX REPLACE ".*NLOPT_([A-Z0-9_]+).*" "\\1" ENUM_STRING ${NLOPT_H_LINE})
string (REGEX REPLACE ".*NLOPT_[A-Z0-9_]+ = (-?[0-9]+).*" "\\1" ENUM_VAL ${NLOPT_H_LINE})
if (ENUM_VAL MATCHES "^-?[0-9]+$")
set (i ${ENUM_VAL})
endif ()
set (ENUM_LINE " integer NLOPT_${ENUM_STRING}\n parameter (NLOPT_${ENUM_STRING}=${i})\n")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.f "${ENUM_LINE}")
# https://public.kitware.com/Bug/print_bug_page.php?bug_id=8996
if (i MATCHES "^-")
math (EXPR i "1 ${i}")
else ()
math (EXPR i "${i} + 1")
endif ()
endif ()
endforeach ()
endif ()
# generate nlopt.hpp from nlopt-in.hpp
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp)
file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "")
file (STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/nlopt-in.hpp NLOPT_HPP_LINES)
foreach (NLOPT_HPP_LINE ${NLOPT_HPP_LINES})
list(LENGTH NLOPT_HPP_LINE line_len)
# handling trailing backlashes in "file (STRINGS" is a little tricky
if (line_len VERSION_LESS 8)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${NLOPT_HPP_LINE}\n")
else ()
set (prev_inst FALSE)
foreach(NLOPT_HPP_SUBLINE ${NLOPT_HPP_LINE})
# test is we need to add the eaten semicolon
if (NLOPT_HPP_SUBLINE MATCHES "\\)$" OR NLOPT_HPP_SUBLINE MATCHES "return")
set (new_inst TRUE)
else ()
set (new_inst FALSE)
endif ()
if (NOT prev_inst)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${NLOPT_HPP_SUBLINE}")
if (new_inst)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp ";")
endif ()
list (FIND NLOPT_HPP_LINE "${NLOPT_HPP_SUBLINE}" index)
math (EXPR index "${index} + 1")
list (LENGTH NLOPT_HPP_LINE total)
if (NOT index STREQUAL total)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " \\")
endif ()
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "\n")
endif ()
set (prev_inst ${new_inst})
endforeach ()
endif ()
if (NLOPT_HPP_LINE MATCHES "GEN_ENUMS_HERE")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " enum algorithm {\n")
file (STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/nlopt.h NLOPT_H_LINES REGEX " NLOPT_[A-Z0-9_]+")
foreach (NLOPT_H_LINE ${NLOPT_H_LINES})
string (REGEX REPLACE "NLOPT_" "" ENUM_LINE ${NLOPT_H_LINE})
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${ENUM_LINE}\n")
if (NLOPT_H_LINE MATCHES "NLOPT_NUM_ALGORITHMS")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " };\n enum result {\n")
elseif (NLOPT_H_LINE MATCHES "NLOPT_NUM_RESULTS")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " };\n")
endif ()
endforeach ()
endif ()
endforeach ()
endif ()
......@@ -6,6 +6,8 @@ if (Matlab_FOUND AND Matlab_MX_LIBRARY)
cmake_minimum_required (VERSION 3.3) # for the matlab_add_mex macro
matlab_add_mex (NAME nlopt_optimize-mex SRC nlopt_optimize-mex.c OUTPUT_NAME nlopt_optimize LINK_TO ${nlopt_lib})
target_include_directories (nlopt_optimize-mex PRIVATE ${PROJECT_SOURCE_DIR}/src/api)
if (CMAKE_VERSION VERSION_LESS 3.14.0)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
message("Forcing mexFunction visibility to default")
......@@ -43,9 +45,8 @@ if (OCTAVE_FOUND)
endforeach ()
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt_optimize_usage.h "\n")
include_directories (${OCTAVE_INCLUDE_DIRS})
include_directories (${CMAKE_CURRENT_BINARY_DIR})
octave_add_oct (nlopt_optimize SOURCES nlopt_optimize-oct.cc ${CMAKE_CURRENT_BINARY_DIR}/nlopt_optimize_usage.h LINK_LIBRARIES ${nlopt_lib})
target_include_directories (nlopt_optimize PRIVATE ${OCTAVE_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/src/api)
if (NOT DEFINED INSTALL_OCT_DIR)
file (RELATIVE_PATH INSTALL_OCT_DIR ${OCTAVE_ROOT_DIR} ${OCTAVE_OCT_SITE_DIR})
......
......@@ -19,7 +19,7 @@ set_source_files_properties (nlopt.i PROPERTIES CPLUSPLUS ON)
if (NUMPY_FOUND AND PYTHONLIBS_FOUND)
set (SWIG_MODULE_nlopt_python_EXTRA_DEPS nlopt-python.i numpy.i)
set (SWIG_MODULE_nlopt_python_EXTRA_DEPS nlopt-python.i numpy.i generate-cpp)
# swig_add_module is deprecated
if (CMAKE_VERSION VERSION_LESS 3.8)
......@@ -35,8 +35,8 @@ if (NUMPY_FOUND AND PYTHONLIBS_FOUND)
set (nlopt_python "nlopt_python")
endif ()
target_include_directories (${nlopt_python} PUBLIC ${PYTHON_INCLUDE_DIRS})
target_include_directories (${nlopt_python} PUBLIC ${NUMPY_INCLUDE_DIRS})
target_include_directories (${nlopt_python} PRIVATE ${PYTHON_INCLUDE_DIRS})
target_include_directories (${nlopt_python} PRIVATE ${NUMPY_INCLUDE_DIRS})
swig_link_libraries (nlopt_python ${nlopt_lib})
target_link_libraries_with_dynamic_lookup (${nlopt_python} ${PYTHON_LIBRARIES})
......@@ -55,7 +55,7 @@ endif ()
if (GUILE_FOUND AND (SWIG_VERSION VERSION_GREATER 2.0.9))
set_source_files_properties (nlopt.i PROPERTIES SWIG_FLAGS "-scmstub")
set (SWIG_MODULE_nlopt_guile_EXTRA_DEPS nlopt-guile.i)
set (SWIG_MODULE_nlopt_guile_EXTRA_DEPS nlopt-guile.i generate-cpp)
# swig_add_module is deprecated
if (CMAKE_VERSION VERSION_LESS 3.8)
......@@ -71,7 +71,7 @@ if (GUILE_FOUND AND (SWIG_VERSION VERSION_GREATER 2.0.9))
set (nlopt_guile "nlopt_guile")
endif ()
target_include_directories (${nlopt_guile} PUBLIC ${GUILE_INCLUDE_DIRS})
target_include_directories (${nlopt_guile} PRIVATE ${GUILE_INCLUDE_DIRS})
swig_link_libraries (nlopt_guile ${nlopt_lib})
target_link_libraries_with_dynamic_lookup (${nlopt_guile} ${GUILE_LIBRARIES})
......
......@@ -77,5 +77,6 @@ endif ()
if (NLOPT_FORTRAN)
add_executable (t_fortran t_fortran.f90)
target_link_libraries (t_fortran ${nlopt_lib})
target_include_directories (t_fortran PRIVATE ${NLOPT_PRIVATE_INCLUDE_DIRS})
add_test (NAME test_fortran COMMAND t_fortran)
endif ()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册