Skip to content

Instantly share code, notes, and snippets.

@autosquid
Created May 9, 2017 06:39
Show Gist options
  • Save autosquid/fcadb21a54e1818f7f397b50778fb533 to your computer and use it in GitHub Desktop.
Save autosquid/fcadb21a54e1818f7f397b50778fb533 to your computer and use it in GitHub Desktop.
A Cmake Template
function(PROTOBUF_GENERATE_PYTHON SRCS)
if(NOT ARGN)
message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
return()
endif()
if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
# Create an include path for each file specified
foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(ABS_PATH ${ABS_FIL} PATH)
list(APPEND _protobuf_include_path -I ${ABS_PATH})
endforeach()
else(PROTOBUF_GENERATE_CPP_APPEND_PATH)
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
endif()
if(DEFINED PROTOBUF_IMPORT_DIRS)
foreach(DIR ${PROTOBUF_IMPORT_DIRS})
get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${ABS_PATH})
endif()
endforeach()
endif()
set(${SRCS})
foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(FIL_WE ${FIL} NAME_WE)
list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py")
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --python_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
COMMENT "Running Python protocol buffer compiler on ${FIL}"
VERBATIM )
endforeach()
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
endfunction()
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/SfM_GlobalPipeline.py.in
${CMAKE_CURRENT_SOURCE_DIR}/SfM_GlobalPipeline.py
@ONLY
)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/SfM_GlobalPipeline.py.in
${CMAKE_CURRENT_SOURCE_DIR}/SfM_GlobalPipeline.py
@ONLY
)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/changetoworkingdir.py.in
${CMAKE_CURRENT_SOURCE_DIR}/changetoworkingdir.py
@ONLY
)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/hardcodes.py.in
${CMAKE_CURRENT_SOURCE_DIR}/hardcodes.py
@ONLY
)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
##### Generate Python Binding ####
# let's make a new target: if .i file changed, let's remove siwg_generated_file first. And make pongeecore depend on this new target (to make sure it builds after this target)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/pongeecore.i"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/pongeecore.i" "${CMAKE_CURRENT_BINARY_DIR}/pongeecore.i.touch"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/pongeecore.i"
COMMENT "check if pongeecore.i changed"
)
add_custom_target(remove_outdated_swig
COMMAND ${CMAKE_COMMAND} -E remove -f ${swig_generated_file_fullname}
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pongeecore.i"
)
set(subdir ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB_RECURSE py_s "${subdir}/*.py")
file(GLOB_RECURSE swig_s "${subdir}/pongeecore.i")
file(GLOB_RECURSE py_in_s "${subdir}/*.py.in")
file(GLOB_RECURSE proto_s "${CMAKE_CURRENT_SOURCE_DIR}/../imp/*.proto")
# this makes a python interface
set (CMAKE_SWIG_OUTDIR ${MyWorkingDirectory})
message(cmake swig output dir ${MyWorkingDirectory})
set_property(SOURCE pongeecore.i PROPERTY CPLUSPLUS ON)
include (UseSWIG)
SWIG_ADD_LIBRARY(pongeecore LANGUAGE python SOURCES ${py_s} ${swig_s} ${proto_s} ${py_in_s})
SWIG_LINK_LIBRARIES(pongeecore ${ALL_LIBS} bamcore)
add_custom_command(TARGET ${SWIG_MODULE_pongeecore_REAL_NAME}
POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${SWIG_MODULE_pongeecore_REAL_NAME}> ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${MyWorkingDirectory}/pongeecore.py ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${SWIG_MODULE_pongeecore_REAL_NAME}> ${MyWorkingDirectory}
COMMENT "copy pyd; fix .py"
)
### Generate Configuration ####
# we need same trick for a clever .py generation
PROTOBUF_GENERATE_PYTHON(PROTO_PY ${proto_s})
add_custom_target(_gen_proto_python
DEPENDS ${PROTO_PY}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PROTO_PY} ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "generate *_pb.py"
)
### Group other python files ###
source_group("\\\\Source" FILES ${cpp_s} ${c_s} ${h_s} ${hpp_s} ${py_s} ${swig_s} ${py_in_s})
# ### arsonist kindof a (prebuild event)
add_dependencies(${SWIG_MODULE_pongeecore_REAL_NAME} remove_outdated_swig _gen_proto_python)
# compare with feeding build process into setup.py, we prefer to do it with `CMake` (agile)
if (WIN32)
set(pongeecore_bin ${MyWorkingDirectory}/${SWIG_MODULE_pongeecore_REAL_NAME}.dll)
else(WIN32)
set(pongeecore_bin ${MyWorkingDirectory}/${SWIG_MODULE_pongeecore_REAL_NAME}.so)
endif(WIN32)
configure_file(setup.py.in ${CMAKE_CURRENT_SOURCE_DIR}/setup.py)
@autosquid
Copy link
Author

including swig, protobuf configurations with dependencies, autochecks and customized commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment