diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 7298575dc..735feb639 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -367,6 +367,18 @@ IF(BUILD_TESTING) SET(CMAKE_TEST_DIFFERENT_GENERATOR TRUE) ENDIF(NOT CMAKE_TEST_GENERATOR) + # Are we testing with the MSVC compiler? + SET(CMAKE_TEST_MSVC 0) + IF(MSVC AND NOT CMAKE_TEST_DIFFERENT_GENERATOR) + SET(CMAKE_TEST_MSVC 1) + ELSE(MSVC AND NOT CMAKE_TEST_DIFFERENT_GENERATOR) + IF("${CMAKE_TEST_GENERATOR}" MATCHES "NMake" OR + "${CMAKE_TEST_GENERATOR}" MATCHES "Visual Studio") + SET(CMAKE_TEST_MSVC 1) + ENDIF("${CMAKE_TEST_GENERATOR}" MATCHES "NMake" OR + "${CMAKE_TEST_GENERATOR}" MATCHES "Visual Studio") + ENDIF(MSVC AND NOT CMAKE_TEST_DIFFERENT_GENERATOR) + SET(CMAKE_TEST_SYSTEM_LIBRARIES 0) FOREACH(util CURL EXPAT XMLRPC ZLIB) IF(CMAKE_USE_SYSTEM_${util}) @@ -1008,6 +1020,18 @@ IF(BUILD_TESTING) ) ENDIF (WIN32) + IF(CMAKE_TEST_MSVC) + ADD_TEST(PrecompiledHeader ${CMAKE_CTEST_COMMAND} + --build-and-test + "${CMake_SOURCE_DIR}/Tests/PrecompiledHeader" + "${CMake_BINARY_DIR}/Tests/PrecompiledHeader" + --build-generator ${CMAKE_TEST_GENERATOR} + --build-project PrecompiledHeader + --build-makeprogram ${CMAKE_TEST_MAKEPROGRAM} + --build-two-config + --test-command foo) + ENDIF(CMAKE_TEST_MSVC) + IF("${CMAKE_TEST_GENERATOR}" MATCHES "Makefile") ADD_TEST(MakeClean ${CMAKE_CTEST_COMMAND} --build-and-test diff --git a/Tests/PrecompiledHeader/CMakeLists.txt b/Tests/PrecompiledHeader/CMakeLists.txt new file mode 100644 index 000000000..ac0e515bd --- /dev/null +++ b/Tests/PrecompiledHeader/CMakeLists.txt @@ -0,0 +1,61 @@ +PROJECT(PrecompiledHeader C) + +# Make sure the proper compiler is in use. +IF(NOT MSVC) + MESSAGE(FATAL_ERROR "The PrecompiledHeader test works only with MSVC") +ENDIF(NOT MSVC) + +# Compute a custom name for the precompiled header. +IF(CMAKE_CONFIGURATION_TYPES) + SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH/${CMAKE_CFG_INTDIR}") +ELSE(CMAKE_CONFIGURATION_TYPES) + SET(PCH_DIR "${CMAKE_CURRENT_BINARY_DIR}/PCH") +ENDIF(CMAKE_CONFIGURATION_TYPES) +FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/PCH) + +# Choose between an explicit include path and using /I during +# precompilation. The /I form is provided as an example. In practice +# the include path form would be used. +SET(PCH_USE_INCLUDE_DIR 1) +IF(PCH_USE_INCLUDE_DIR) + INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) +ELSE(PCH_USE_INCLUDE_DIR) + SET(PCH_INCLUDE_DIR "\"/I${CMAKE_CURRENT_SOURCE_DIR}/include\"") +ENDIF(PCH_USE_INCLUDE_DIR) + +# The VS6 IDE does not support renaming .pch files so we cannot use a +# separate target. +IF(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio 6") + SET(PCH_USE_TARGET 1) +ENDIF(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio 6") + +# Create a target that will use a precompiled header. +SET(foo_SRCS foo1.c foo2.c) +IF(PCH_USE_TARGET) + ADD_EXECUTABLE(foo ${foo_SRCS}) + + # Create a target to precompile the header for the executable. + ADD_LIBRARY(foo_precompile foo_precompile.c include/foo_precompiled.h) + SET_TARGET_PROPERTIES(foo_precompile PROPERTIES OUTPUT_NAME foo) + ADD_DEPENDENCIES(foo foo_precompile) + + SET(PCH_TARGETS foo foo_precompile) + SET(PCH_FILE "\"/Fp${PCH_DIR}/foo_precompiled.pch\"") +ELSE(PCH_USE_TARGET) + # Put the precompiled header source directly in the target. + ADD_EXECUTABLE(foo foo_precompile.c ${foo_SRCS}) + SET(PCH_TARGETS foo) +ENDIF(PCH_USE_TARGET) + +# Setup flags on the two targets to create and use the precompiled header. +SET_TARGET_PROPERTIES(${PCH_TARGETS} PROPERTIES COMPILE_FLAGS + "/Yufoo_precompiled.h /FIfoo_precompiled.h ${PCH_FILE}") +SET_SOURCE_FILES_PROPERTIES(foo_precompile.c PROPERTIES COMPILE_FLAGS + "/Ycfoo_precompiled.h ${PCH_INCLUDE_DIR}") + +# Make sure the object files rebuild when their precompiled header has changed. +# The VS IDE takes care of this automatically. +IF("${CMAKE_GENERATOR}" MATCHES "Makefile") + SET_SOURCE_FILES_PROPERTIES(${foo_SRCS} PROPERTIES + OBJECT_DEPENDS "${PCH_DIR}/foo_precompiled.pch") +ENDIF("${CMAKE_GENERATOR}" MATCHES "Makefile") diff --git a/Tests/PrecompiledHeader/foo1.c b/Tests/PrecompiledHeader/foo1.c new file mode 100644 index 000000000..b10eba7e9 --- /dev/null +++ b/Tests/PrecompiledHeader/foo1.c @@ -0,0 +1,8 @@ +#ifndef foo_h +# error "Precompiled header foo_precompiled.h has not been loaded." +#endif + +int main() +{ + return foo(); +} diff --git a/Tests/PrecompiledHeader/foo2.c b/Tests/PrecompiledHeader/foo2.c new file mode 100644 index 000000000..2845cdb15 --- /dev/null +++ b/Tests/PrecompiledHeader/foo2.c @@ -0,0 +1,9 @@ +#ifndef foo_h +# include "foo.h" +# error "Precompiled header foo_precompiled.h has not been loaded." +#endif + +int foo() +{ + return 0; +} diff --git a/Tests/PrecompiledHeader/foo_precompile.c b/Tests/PrecompiledHeader/foo_precompile.c new file mode 100644 index 000000000..c37d69a1c --- /dev/null +++ b/Tests/PrecompiledHeader/foo_precompile.c @@ -0,0 +1,5 @@ +/* The foo_precompiled.h header is included by a /FI option when this + source is used to create a precompiled header. Include it here + explicitly to allow dependency scanning to detect the dependency + whether or not the include path is known to the scanner. */ +#include "include/foo_precompiled.h" diff --git a/Tests/PrecompiledHeader/include/foo.h b/Tests/PrecompiledHeader/include/foo.h new file mode 100644 index 000000000..2210cb41d --- /dev/null +++ b/Tests/PrecompiledHeader/include/foo.h @@ -0,0 +1,4 @@ +#ifndef foo_h +#define foo_h +extern int foo(); +#endif diff --git a/Tests/PrecompiledHeader/include/foo_precompiled.h b/Tests/PrecompiledHeader/include/foo_precompiled.h new file mode 100644 index 000000000..f4de601ff --- /dev/null +++ b/Tests/PrecompiledHeader/include/foo_precompiled.h @@ -0,0 +1 @@ +#include "foo.h"