Skip to content

Instantly share code, notes, and snippets.

@schuhschuh
Last active January 14, 2016 21:45
Show Gist options
  • Save schuhschuh/bee9c31ca4a6a8879946 to your computer and use it in GitHub Desktop.
Save schuhschuh/bee9c31ca4a6a8879946 to your computer and use it in GitHub Desktop.
GCoptimiztion (GCO) CMake build files
# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/
# Author: Andreas Schuh
# Minimum required CMake version and policies
cmake_minimum_required(VERSION 2.8.12)
if (POLICY CMP0042)
cmake_policy(SET CMP0042 NEW)
endif ()
# Project setup
project(GCO)
string(TOLOWER ${PROJECT_NAME} project_name)
set(GCO_VERSION_MAJOR 3)
set(GCO_VERSION_MINOR 0)
set(GCO_VERSION_PATCH 0)
set(GCO_VERSION "${GCO_VERSION_MAJOR}.${GCO_VERSION_MINOR}.${GCO_VERSION_PATCH}")
option(BUILD_SHARED_LIBS "Request build as shared library (Unix) or DLL (Windows)" ON)
if (NOT CMAKE_BUILD_TYPE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY VALUE Release)
endif ()
# List of library source files
set(HEADERS
block.h
energy.h
graph.h
graph.cpp # yes, seriously...
maxflow.cpp
LinkedBlockList.h
GCoptimization.h
)
set(SOURCES
LinkedBlockList.cpp
GCoptimization.cpp
)
# Copy header files to build directory with include path identical to installation
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/include/${project_name}")
set(_HEADERS)
foreach (header IN LISTS HEADERS)
set(header_copy "${PROJECT_BINARY_DIR}/include/${project_name}/${header}")
add_custom_command(
OUTPUT "${header_copy}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${PROJECT_SOURCE_DIR}/${header}" "${header_copy}"
COMMENT "Copy ${header} to include/${project_name}/"
)
list(APPEND _HEADERS "${header_copy}")
endforeach ()
# Add build target
add_library(${project_name} ${_HEADERS} ${SOURCES})
set_target_properties(${project_name} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY lib
LIBRARY_OUTPUT_DIRECTORY lib
VERSION ${GCO_VERSION}
SOVERSION ${GCO_VERSION_MAJOR}
)
target_include_directories(${project_name} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
# Install project files
install(TARGETS ${project_name} EXPORT dev
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
)
install(FILES ${HEADERS} DESTINATION include/${project_name})
export(EXPORT dev FILE ${PROJECT_NAME}Targets.cmake)
install(EXPORT dev
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION lib/cmake/${project_name}
)
# Generate and install package configuration file
configure_file(Config.cmake.in "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" @ONLY)
configure_file(ConfigInstall.cmake.in "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigInstall.cmake" @ONLY)
install(
FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigInstall.cmake"
RENAME ${PROJECT_NAME}Config.cmake
DESTINATION lib/cmake/${project_name}
)
# Generate and install package configuration version file
include(WriteBasicConfigVersionFile)
write_basic_config_version_file(${PROJECT_NAME}ConfigVersion.cmake
VERSION ${GCO_VERSION}
COMPATIBILITY AnyNewerVersion
)
install(FILES
${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION lib/cmake/${project_name}
)
# Compute the build tree prefix relative to this file
get_filename_component(_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
# Import exported targets
include("${_PREFIX}/@PROJECT_NAME@Targets.cmake")
# Set user variables
set(@PROJECT_NAME@_INCLUDE_DIRS "${_PREFIX}/include")
set(@PROJECT_NAME@_LIBRARY "@project_name@")
# Compute the installation prefix relative to this file
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_PREFIX "${_PREFIX}" PATH)
get_filename_component(_PREFIX "${_PREFIX}" PATH)
# Import exported targets
include("${_IMPORT_PREFIX}/@PROJECT_NAME@Targets.cmake")
# Set user variables
set(@PROJECT_NAME@_INCLUDE_DIRS "${_PREFIX}/include")
set(@PROJECT_NAME@_LIBRARY "@project_name@")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment