[CMake] Testing and DLLs

John Drescher drescherjm at gmail.com
Wed May 4 11:52:10 EDT 2011


> I am curious whether there is a common way of dealing with unit tests
> when the actual project to be tested is a DLL? The issue I am facing
> is a common Windows issue where the required DLL is not found in the
> path.
>
> I tried to put all binaries in the same directory by modifying
> CMAKE_*_OUTPUT_DIRECTORY but that did not affect my tests (added via
> add_test).
>
> Another solution I can think of is running "make INSTALL" before
> running the tests but that feels like a strange approach since I
> typically want to test before installing.
>
> Its also not really possible to adapt the PATH environment variable -
> how could I possible guess where somebody is going to build my code!?
>
> So again, I am really curious if there is a well working solution.
>

I now have a custom build script that generates a batch file that
copies (using cmake commands) the required dlls into the Debug,
Release, RelWithDebInfo folders for me for the few libraries that
require dlls (Qt ). For ITK, VTK ... I use static libs so no need to
copy these.





option (GET_RUNTIME			"Create a target that will get the runtime" ON)

IF (GET_RUNTIME)

	SET (${PROJECT_NAME}_PACKAGING_RUNTIME "")

	SET (RUNTIME_BATCH_FILENAME "${PROJECT_BINARY_DIR}/GetRuntime.bat"
CACHE FILEPATH "${PROJECT_BINARY_DIR}/GetRuntime.bat")
	
	add_custom_target(GetRuntime  "${RUNTIME_BATCH_FILENAME}")
	
#########################################################################################

	macro( add_runtime_file BatchFileName RuntimeFile Configuration )

		# Test if this is a valid configuration.
		STRING( REGEX MATCH ${Configuration} __MATCHED__ ${CMAKE_CONFIGURATION_TYPES})
		if ( NOT "${__MATCHED__}" STREQUAL "" )
		
			IF (NOT EXISTS "${EXECUTABLE_OUTPUT_PATH}/${Configuration}")
				file( MAKE_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}/${Configuration}" )
			ENDIF (NOT EXISTS "${EXECUTABLE_OUTPUT_PATH}/${Configuration}")
		
			GET_FILENAME_COMPONENT( BatchFile ${BatchFileName} NAME_WE )
			
			#The following will truncate the file on the first call to add_runtime_file.
			if ( NOT DEFINED __add_runtime_file_${BatchFile}__ )
				set ( __add_runtime_file_${BatchFile}__ 1)
				file( WRITE ${BatchFileName} "REM This file will copy the runtimes
to the Debug and Release binary folders\n" )
			endif ( NOT DEFINED __add_runtime_file_${BatchFile}__)
			
			#The following line will add the entry in the batch file for
copying the Runtime file to the folder
${PROJECT_BINARY_DIR}/${Configuration}/
			file( APPEND ${BatchFileName} "\"${CMAKE_COMMAND}\" -E
copy_if_different \"${RuntimeFile}\"
\"${EXECUTABLE_OUTPUT_PATH}/${Configuration}/\"\n" )
		endif(  NOT "${__MATCHED__}" STREQUAL "" )
	endmacro( add_runtime_file )
	
#########################################################################################

	macro( add_runtime_file_for_packaging RuntimeFile )
		SET( ${PROJECT_NAME}_PACKAGING_RUNTIME
${${PROJECT_NAME}_PACKAGING_RUNTIME} ${RuntimeFile} )
	endmacro( add_runtime_file_for_packaging )
	
ENDIF(GET_RUNTIME)


More information about the CMake mailing list