Skip to content

Commit

Permalink
Further CMake build scripts cleanup.
Browse files Browse the repository at this point in the history
Finds optional documentation packages only if documentation target is
registered.  Removes circular dependency between root CMakeLists file
and CMake package configuration file.  Adds support for selecting MSVC
runtime library linkage mode (facilitates embedding in projects that
might require a specific linkage mode for 3rd-party libraries).
  • Loading branch information
AndreLouisCaron committed Apr 12, 2012
1 parent fec5f48 commit 04626cf
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 62 deletions.
88 changes: 26 additions & 62 deletions CMakeLists.txt
Expand Up @@ -28,56 +28,12 @@ cmake_minimum_required(VERSION 2.6)

project(w32++)

# Locate documentation tools.
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package(java QUIET)
find_package(plantuml QUIET)
find_package(doxygen)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY
set(CMAKE_MODULE_PATH
${CMAKE_CURRENT_SOURCE_DIR}/site
)

# Render PlantUML diagrams to PNG.
function(add_diagram target source)
if(Java_JAVA_EXECUTABLE AND PLANTUML_JARFILE)
set(plantuml ${Java_JAVA_EXECUTABLE} -jar ${PLANTUML_JARFILE})
get_filename_component(output ${source} NAME_WE)
add_custom_command(
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/${output}.png
COMMAND
${plantuml} -o ${CMAKE_CURRENT_BINARY_DIR} -tpng ${source}
MAIN_DEPENDENCY
${source}
COMMENT
"Rendering UML diagram '${output}'."
)
add_custom_target(${target}
ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${output}.png
)
endif()
endfunction()

# Compile API documentation from source code.
function(add_api_documentation target)
if(DOXYGEN_EXECUTABLE)
add_custom_target(
${target}
COMMAND
${DOXYGEN_EXECUTABLE}
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
COMMENT
"Compiling documentation."
VERBATIM
)
endif()
endfunction()

# Define usual include paths, etc.
set(w32_DIR ${CMAKE_CURRENT_SOURCE_DIR})
find_package(w32 REQUIRED)
find_package(MSVCExtras)
msvc_configure_runtime()
msvc_enable_se_handling()

if(MSVC)
# C and C++ runtime libraries are safe to use.
Expand All @@ -86,36 +42,44 @@ if(MSVC)
add_definitions(
-D_SCL_SECURE_NO_WARNINGS
-D_CRT_SECURE_NO_WARNINGS
)
# Configure Windows SDK headers.
add_definitions(
-D_WIN32_WINNT=0x601
-DSECURITY_WIN32
)
# Enable structured exception handling to support
# translation of hardware exceptions to C++
# exceptions.
set(CMAKE_CXX_FLAGS
"/DWIN32 /D_WINDOWS /EHsc /WX /wd4355 /wd4251 /wd4250 /wd4996"
CACHE STRING "Release compiler flags" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG
"/DWIN32 /D_WINDOWS /EHsc /WX /wd4355 /wd4251 /wd4250 /wd4996"
CACHE STRING "Debug compiler flags" FORCE)
endif()

# Put all libraries and executables in the build folder root.
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})

# Resolve our own code in '#include <...>' directives.
include_directories(${w32_include_dir})
# Resolve our own library headers.
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/code
)

# Build all libraries.
# Build the core libraries.
add_subdirectory(code)

# When building in standalone mode, build demo projects and documentation.
# Exclude all but the core libraries when built as a dependency.
if(${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME})

# Build demo projects.
add_subdirectory(demo)

# Locate documentation tools.
find_package(java QUIET)
find_package(plantuml QUIET)
find_package(PlantUMLExtras)
find_package(doxygen)
find_package(DoxygenExtras)

# Compile API documentation.
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY
)
add_api_documentation(help)

endif()
15 changes: 15 additions & 0 deletions site/FindDoxygenExtras.cmake
@@ -0,0 +1,15 @@
# Compile API documentation from source code.
function(add_api_documentation target)
if(DOXYGEN_EXECUTABLE)
add_custom_target(
${target}
COMMAND
${DOXYGEN_EXECUTABLE}
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
COMMENT
"Compiling documentation."
VERBATIM
)
endif()
endfunction()
88 changes: 88 additions & 0 deletions site/FindMSVCExtras.cmake
@@ -0,0 +1,88 @@
# Copyright (c) 2009-2012, Andre Caron (andre.l.caron@gmail.com)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# List CMake variables containing build flags.
set(CMAKE_COMPILER_FLAGS_VARIABLES
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
)

# Select static or dynamic MSVC runtime based on 'MSVC_RUNTIME' variable.
# This variable can be set through the command-line like so:
#
# cmake -G "..." -DMSVC_RUNTIME=static
#
# Acceptable values are "static" (default) and "dynamic".
macro(msvc_configure_runtime)
if(MSVC)
# Default to statically-linked runtime.
if("${MSVC_RUNTIME}" STREQUAL "")
set(MSVC_RUNTIME "static")
endif()
# Set compiler options.
if(${MSVC_RUNTIME} STREQUAL "static")
message(STATUS
"MSVC -> forcing use of statically-linked runtime."
)
foreach(variable ${CMAKE_COMPILER_FLAGS_VARIABLES})
if(${variable} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}")
endif()
endforeach()
else()
message(STATUS
"MSVC -> forcing use of dynamically-linked runtime."
)
foreach(variable ${CMAKE_COMPILER_FLAGS_VARIABLES})
if(${variable} MATCHES "/MT")
string(REGEX REPLACE "/MT" "/MD" ${variable} "${${variable}}")
endif()
endforeach()
endif()
endif()
endmacro()

# Enable Microsoft's structured exception handling.
macro(msvc_enable_se_handling)
if(MSVC)
message(STATUS
"MSVC -> enabling structured exception hanlding."
)
if(CMAKE_CXX_FLAGS MATCHES "/EH")
string(REGEX REPLACE
"/EH[acs]+" "/EHa" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}"
)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa")
endif()
endif()
endmacro()
File renamed without changes.
20 changes: 20 additions & 0 deletions site/FindPlantUMLExtras.cmake
@@ -0,0 +1,20 @@
# Render PlantUML diagrams to PNG.
function(add_diagram target source)
if(Java_JAVA_EXECUTABLE AND PLANTUML_JARFILE)
set(plantuml ${Java_JAVA_EXECUTABLE} -jar ${PLANTUML_JARFILE})
get_filename_component(output ${source} NAME_WE)
add_custom_command(
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/${output}.png
COMMAND
${plantuml} -o ${CMAKE_CURRENT_BINARY_DIR} -tpng ${source}
MAIN_DEPENDENCY
${source}
COMMENT
"Rendering UML diagram '${output}'."
)
add_custom_target(${target}
ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${output}.png
)
endif()
endfunction()

0 comments on commit 04626cf

Please sign in to comment.