pull_extra_module.cmake 5.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18


set(SCRIPT_CLONE_MODULE "${CMAKE_CURRENT_SOURCE_DIR}/scriptCloneModule.sh" CACHE STRING "Script to be used to clone project used as module")
set(SCRIPT_PULL_MODULE "${CMAKE_CURRENT_SOURCE_DIR}/scriptPull.sh" CACHE STRING "Script to be used to clone project used as module")
set(SCRIPT_CLONE_PACKAGE "${CMAKE_CURRENT_SOURCE_DIR}/scriptClonePackage.sh" CACHE STRING "Script to be used to clone package used as module")

# Check if a project (used as module) is found, if it's not, it will be fetch with its url
# When the project is found, this function does a classic add_subdirectory
# Parameter :
# 	packageName : name of the package to be found (classic parameter of find_package)
# 	urlGitToPackage : url of the git repository to be used to fetch it if it is not found
function(get_extra_module packageName urlGitToPackage)
	set(PHOENIX_TARGET_NAME "phoenix_${packageName}_found")

	if(TARGET ${PHOENIX_TARGET_NAME})
		message(STATUS "Project ${packageName} already found, target ${PHOENIX_TARGET_NAME} exits")
	else()
		add_custom_target(${PHOENIX_TARGET_NAME})
Pierre Aubert's avatar
Pierre Aubert committed
19
		set(SUB_MODULE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tmp_project)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		file(MAKE_DIRECTORY ${SUB_MODULE_DIRECTORY})
		set(MODULE_MODE yes)
		set(MODULE_PROJECT_DIR ${SUB_MODULE_DIRECTORY}/${packageName})
		if(EXISTS ${MODULE_PROJECT_DIR})
			message(STATUS "Find project ${packageName} in ${MODULE_PROJECT_DIR}")
			
			execute_process(COMMAND ${SCRIPT_PULL_MODULE} ${MODULE_PROJECT_DIR}
						WORKING_DIRECTORY ${SUB_MODULE_DIRECTORY}/
						ERROR_QUIET
						OUTPUT_QUIET
						)
			add_subdirectory(${MODULE_PROJECT_DIR})
			include_directories(${MODULE_PROJECT_DIR}/src)
			include_directories(${PHOENIX_INCLUDE_DIRS})
		else()
			message(STATUS "Project ${packageName} not found")
			if(urlGitToPackage)
				get_filename_component(basePackageDirName ${urlGitToPackage} NAME_WE)
				message(STATUS "Get project ${packageName} with url '${urlGitToPackage}'")
				execute_process(COMMAND ${SCRIPT_CLONE_MODULE} ${urlGitToPackage}
						WORKING_DIRECTORY ${SUB_MODULE_DIRECTORY}/)
				if(EXISTS ${MODULE_PROJECT_DIR})
					message(STATUS "Find project ${packageName} in ${MODULE_PROJECT_DIR}")
					add_subdirectory(${MODULE_PROJECT_DIR})
					include_directories(${MODULE_PROJECT_DIR}/src)
					include_directories(${PHOENIX_INCLUDE_DIRS})
				else()
					message(FATAL_ERROR "Project ${packageName} not found even after cloning repository ${urlGitToPackage}")
				endif()
			else(urlGitToPackage)
				message(FATAL_ERROR "Package ${packageName} not found and no url to fetch it")
			endif(urlGitToPackage)
		endif()
	endif()
endfunction(get_extra_module)


# Check if a project (used as module) is found, if it's not, it will be fetch with its url
# When the project is found, this function does a classic add_subdirectory
# Parameter :
# 	packageName : name of the package to be found (classic parameter of find_package)
# 	urlGitToPackage : url of the git repository to be used to fetch it if it is not found
function(pull_extra_module packageName urlGitToPackage)
# 	There  is one global varialbe PHOENIX_FORCE_PULL_MODULE
# 		- if the PHOENIX_FORCE_PULL_MODULE == no
# 			- we look for the FindXXX.cmake and we use the classic find_package
# 				- if the package has been found nothing to do
# 					- but if the corresponding module is found, we remove it (to avoid extra clone and conflict)
# 				- if the package is not found, we get the existing module (by clone or pull)
# 					- if the module does not exsit, it is cloned (and then potentially installed)
# 					- if the module does exsit, it is pulled (and also potentially installed after)
# 		- if the PHOENIX_FORCE_PULL_MODULE == yes
# 			- if the module is not found, we clone it even if the package does exist (we recompile it and reinstall it)
# 			- if the module is found, we pull it even if the package does exist (we recompile it and reinstall it)
	if(PHOENIX_FORCE_PULL_MODULE)
		# We always pull the module
		get_extra_module(${packageName} ${urlGitToPackage})
	else()
# 		Try to find the package
		find_package(${packageName})
		string(TOUPPER ${packageName} UPPER_PACKAGE_NAME)
		if(${${UPPER_PACKAGE_NAME}_FOUND})
# 			The package is found, we check if the module exist
Pierre Aubert's avatar
Pierre Aubert committed
83
			set(MODULE_PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tmp_project/${packageName})
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
			if(EXISTS ${MODULE_PROJECT_DIR})
				message(STATUS "Remove module ${packageName} in ${MODULE_PROJECT_DIR}")
				file(REMOVE_RECURSE ${MODULE_PROJECT_DIR})
			endif()
		else()
# 			The module is not found, so we get it
			message(STATUS "${packageName} not found")
			get_extra_module(${packageName} ${urlGitToPackage})
		endif()
	endif()
endfunction(pull_extra_module)


# Do a classic find_package but if the package is not found, it will be fetch with its url
# Parameter :
# 	packageName : name of the package to be found (classic parameter of find_package)
# 	urlGitToPackage : url of the git repository to be used to fetch it if it is not found
function(phoenix_find_package packageName urlGitToPackage)
	find_package(${packageName})
	string(TOUPPER ${packageName} UPPER_PACKAGE_NAME)
	message(STATUS "FIND variable ${packageName}_FOUND = ${${UPPER_PACKAGE_NAME}_FOUND}")
	if(NOT ${${UPPER_PACKAGE_NAME}_FOUND})
		message(STATUS "${packageName} not found")
		if(urlGitToPackage)
			get_filename_component(basePackageDirName ${urlGitToPackage} NAME_WE)
			message(STATUS "The url '${urlGitToPackage}' gives basePackageDirName = ${basePackageDirName}")
			
			execute_process(COMMAND ${SCRIPT_CLONE_PACKAGE} ${basePackageDirName} ${urlGitToPackage} ${CMAKE_INSTALL_PREFIX}
					WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
			
			message(STATUS "End of ${basePackageDirName} installation")
			find_package(${packageName})
		else(urlGitToPackage)
			message(FATAL_ERROR "Package ${packageName} not found and no url to fetch it")
		endif(urlGitToPackage)
	endif()
endfunction(phoenix_find_package)