[CMake] -fPIC and precompiled header

Axel Roebel Axel.Roebel at ircam.fr
Sat Jan 6 11:18:39 EST 2007


On Saturday 06 January 2007 16:05, Axel Roebel wrote:
> On Saturday 06 January 2007 11:29, you wrote:
> > I will merge your changes today. Many thanks!
> >
> > NOTE:
> > Consider that I'm not a maintainer. I'm just a developer who was annoyed
> > by the long compile times when using boost. Feel encouraged to make
> > changes to fulfill your needs and keep on posting the resulting
> > cmake-file. I will do my very best to merge your and others
> > modifications into one cmake file.
>
> Don't bother I have these changes, I'll post them
> soon. The last thing I'd like to add is correct handling
> of distcc which is currently not working with
> your version of PCHSupport.cmake.
>
> Cheers,
Hi Maik,

Find attached my modified version. 

The changes applied are all the comments from my previous mail
(the compiler test is still sub optimal but as I am not using windows...)

Moreover I simplified the compiler version determination
and fixed PCHSupport.cmake such that you can use it with
distcc as in

CC="distcc /usr/local/bin/gcc" CXX="distcc /usr/local/bin/gcc" cmake ..

BTW, if you have more then one computer available and you suffer from
long compilation times distcc is really worth it! I distribute
compilation even over internet to my company and I still get 
compile time reduction of 25%.

I hope these changes work for you as well.

Bis demnaechst,

-- 
Axel Roebel
IRCAM Analysis/Synthesis Team
Phone: ++33-1-4478 4845 | Fax: ++33-1-4478 1540
-------------- next part --------------
# - Try to find precompiled headers support for GCC 3.4 and 4.x
# Once done this will define:
#
# Variable:
#   PCHSupport_FOUND
#
# Macro:
#   ADD_PRECOMPILED_HEADER

IF(CMAKE_COMPILER_IS_GNUCXX)

    EXEC_PROGRAM(
    	${CMAKE_CXX_COMPILER}  
        ARGS 	${CMAKE_CXX_COMPILER_ARG1} -dumpversion 
        OUTPUT_VARIABLE gcc_compiler_version)
    MESSAGE("GCC Version: ${gcc_compiler_version}")
    IF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
        SET(PCHSupport_FOUND TRUE)
    ELSE(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
        IF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
            SET(PCHSupport_FOUND TRUE)
        ENDIF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
    ENDIF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
    
	SET(_PCH_include_prefix "-I")
	
ELSE(CMAKE_COMPILER_IS_GNUCXX)
IF(WIN32)	
	SET(PCHSupport_FOUND TRUE) # for experimental msvc support
	SET(_PCH_include_prefix "/I")
ELSE(WIN32)
	SET(PCHSupport_FOUND FALSE)
ENDIF(WIN32)	
ENDIF(CMAKE_COMPILER_IS_GNUCXX)


MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)


  STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
  SET(${_out_compile_flags} ${${_flags_var_name}} )
  
  IF(CMAKE_COMPILER_IS_GNUCXX)
    
    GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
    IF(${_targetType} STREQUAL SHARED_LIBRARY)
      LIST(APPEND ${_out_compile_flags} "${${_out_compile_flags}} -fPIC")
    ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)
    
  ELSE(CMAKE_COMPILER_IS_GNUCXX)
    ## TODO ... ? or does it work out of the box 
  ENDIF(CMAKE_COMPILER_IS_GNUCXX)
  
  GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
  FOREACH(item ${DIRINC})
    LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}${item}")
  ENDFOREACH(item)
  
  GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
  #MESSAGE("_directory_flags ${_directory_flags}" )
  LIST(APPEND ${_out_compile_flags} ${_directory_flags})
  LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS} )
  
  SEPARATE_ARGUMENTS(${_out_compile_flags})

ENDMACRO(_PCH_GET_COMPILE_FLAGS)



MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)

	FILE(TO_NATIVE_PATH ${_input} _native_input)
	FILE(TO_NATIVE_PATH ${_output} _native_output)
	

	IF(CMAKE_COMPILER_IS_GNUCXX)
          IF(CMAKE_CXX_COMPILER_ARG1)
	    # remove leading space in compiler argument
            STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})

	    SET(${out_command} 
	      ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS}	-x c++-header -o ${_output} ${_input} 
	      )
	  ELSE(CMAKE_CXX_COMPILER_ARG1)
	    SET(${out_command} 
	      ${CMAKE_CXX_COMPILER}  ${_compile_FLAGS}	-x c++-header -o ${_output} ${_input} 
	      )
          ENDIF(CMAKE_CXX_COMPILER_ARG1)
	ELSE(CMAKE_COMPILER_IS_GNUCXX)
		
		SET(_dummy_str "#include <${_input}>")
		FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str})
	
		SET(${out_command} 
			${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp
		)	
		#/out:${_output}

	ENDIF(CMAKE_COMPILER_IS_GNUCXX)
	
ENDMACRO(_PCH_GET_COMPILE_COMMAND )



MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags  _header_name _pch_path  )

	FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
	message(${_native_pch_path})

	IF(CMAKE_COMPILER_IS_GNUCXX)
	
		set(${_cflags} "-include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} -Winvalid-pch" )
	
	ELSE(CMAKE_COMPILER_IS_GNUCXX)

		set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )	

	ENDIF(CMAKE_COMPILER_IS_GNUCXX)	

ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )



MACRO(ADD_PRECOMPILED_HEADER _targetName _input )

  SET(_PCH_current_target ${_targetName})
  
  IF(NOT CMAKE_BUILD_TYPE)
    MESSAGE(FATAL_ERROR 
      "This is the ADD_PRECOMPILED_HEADER macro. " 
      "You must set CMAKE_BUILD_TYPE!"
      )
  ENDIF(NOT CMAKE_BUILD_TYPE)

  GET_FILENAME_COMPONENT(_name ${_input} NAME)
  GET_FILENAME_COMPONENT(_path ${_input} PATH)
  SET(_outdir "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch")
  SET(_output "${_outdir}/${_targetName}_${CMAKE_BUILD_TYPE}.h++")
  

  GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
  SET(PHC_MASTER_INCLUDE_FILE ${_input})
  CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/cmModules/PCHCheckDependencies.cxx.in ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_gchdep.cxx)
  IF(${_targetType} STREQUAL SHARED_LIBRARY)
    ADD_LIBRARY(${_targetName}_gchdep SHARED  ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_gchdep.cxx)
  ELSE(${_targetType} STREQUAL SHARED_LIBRARY)
    ADD_LIBRARY(${_targetName}_gchdep STATIC ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_gchdep.cxx)
  ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)

  ADD_CUSTOM_COMMAND(
    OUTPUT ${_outdir}
    COMMAND mkdir ${_outdir} # TODO: {CMAKE_COMMAND} -E ... !doesn't work in win32
    )
  
  _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
  
  #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
  #message("COMMAND ${CMAKE_CXX_COMPILER}	${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
  
ADD_CUSTOM_COMMAND(
   OUTPUT	${CMAKE_CURRENT_BINARY_DIR}/${_name} 
   COMMAND ${CMAKE_COMMAND} -E copy  ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} # ensure same directory! Required by gcc
  )
  
  #message("_command  ${_input} ${_output}")
  _PCH_GET_COMPILE_COMMAND(_command  ${_input} ${_output} )
  
  #message(${_input} )
#  message("${_command}")

   ADD_CUSTOM_COMMAND(
   OUTPUT ${_output} 	
   COMMAND ${_command}
   DEPENDS ${_input} ${_outdir} ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_targetName}_gchdep
   )

   ADD_CUSTOM_TARGET(${_targetName}_gch 
     DEPENDS	${_output} 	
     )
   
   ADD_DEPENDENCIES(${_targetName} ${_targetName}_gch )
  
  
   _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_output})
   
   SET_TARGET_PROPERTIES(${_targetName} 
     PROPERTIES	
     COMPILE_FLAGS ${_target_cflags} 
     )
	
	
ENDMACRO(ADD_PRECOMPILED_HEADER)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PCHCheckDependencies.cxx.in
Type: text/x-csrc
Size: 98 bytes
Desc: not available
Url : http://public.kitware.com/pipermail/cmake/attachments/20070106/845948fd/PCHCheckDependencies.cxx.c


More information about the CMake mailing list