Change cmake submodule macros#17
Conversation
|
I think we can be more aggressive in these changes. If we do things cleanly, there shouldn't be a use case for initializing all submodules in add_submodule(fmt)
if (${PROJECT_NAME}_BUILD_TESTING)
add_submodule(googletest gtest)
endif()By default, this macro will assume that the module, the directory, and the primary target all share the same name. If that's not the case, we can use additional arguments to make the appropriate distinctions. The macro itself will look something like this: macro(add_submodule module_name)
if (NOT TARGET module_name) # Don't clone or add subdirectory if the module/target already exists
update_submodule(module_name) # Maybe this could be expanded in this macro instead of being a separate macro?
add_subdirectory(module_name)
endif()
endmacro()We could also adapt this to be more general to pass in:
See this article on how to handle extra arguments. Let me know if I need to clarify anything here. |
nealkruis
left a comment
There was a problem hiding this comment.
See comments in conversation thread...
| set(MacroArgs ${ARGN}) | ||
| list(LENGTH MacroArgs NumArgs) | ||
|
|
||
| set(have_submodule FALSE) |
There was a problem hiding this comment.
This doesn't appear to be used anywhere.
There was a problem hiding this comment.
Used to count arguments. This has been rearranged.
| if (NOT is_submodule) | ||
| string(COMPARE EQUAL ${line} "[submodule \"${module_ref_name}\"]" is_submodule) | ||
| if (is_submodule) | ||
| message(STATUS "\"${module_name}\" is a submodule of this project.") |
| set(git_modules_file "${PROJECT_SOURCE_DIR}/.gitmodules") | ||
| if (EXISTS ${git_modules_file}) | ||
| file(STRINGS ${git_modules_file} file_lines) | ||
| set(have_path FALSE) |
There was a problem hiding this comment.
This was already set to FALSE before.
There was a problem hiding this comment.
but may be been changed within the loop
| endif() | ||
| endif() | ||
| else() | ||
| message(STATUS "\"${module_name}\" is not a submodule of this project") |
There was a problem hiding this comment.
Shouldn't this be a FATAL_ERROR? Or maybe separate fatal errors for each situation:
- It isn't a submodule in .gitmodules: "Submodule '${module_name}' not found."
- It is a submodule, but you couldn't find the path: "Submodule '${module_name}' path not found."
There was a problem hiding this comment.
In this case the module has not been added as a git submodule for this project, but may be a submodule of another submodule. For example, HPWHsim uses googletest via btwxt.
|
|
||
| if(is_submodule) | ||
| if(have_path) | ||
| message(STATUS "Updating submodule \"${module_name}\" at ${submodule_path}") |
There was a problem hiding this comment.
I think this is where we want a status message since cloning can take a while and it explains what's happening on stdout. Generally, this should only happen for the initial clone of the repository (and not for updates). Maybe a better message would be: "Initializing ${submodule} submodule"?
| WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
| RESULT_VARIABLE GIT_SUBMOD_RESULT) | ||
| if(GIT_SUBMOD_RESULT EQUAL "0") | ||
| message(STATUS "Successfully updated submodule \"${module_name}\"") |
| endif() | ||
|
|
||
| if (TARGET ${module_name}) | ||
| message(STATUS "Submodule \"${module_name}\" is a target.") |
| message(STATUS "Submodule \"${module_name}\" is a target.") | ||
| else() | ||
| if(EXISTS "${module_path}") | ||
| message(STATUS "Adding subdirectory ${module_path} to this project") |
| endif() | ||
| endmacro() No newline at end of file | ||
|
|
||
| message("") |
There was a problem hiding this comment.
Give this version a try in a few situations and feel free to change the messaging however you prefer.
|
@spahrenk I made some changes to this PR to include the other capabilities mentioned in the comments. Can you please review this and test this approach in HPWHsim? |
Description
CMake macros were added/altered to enable optional submodule initialization. Previously, all submodules listed in .gitmodules were updated by default. For use of HPWHsim by cse, it became evident that submodules used by HPWHsim for development and testing may not be needed in projects where use is limited to the HPWHsim library only, and it is preferable to disable automatic updating of those submodules when building cse. Those updates can be made conditional through individual, qualified calls to
initialize_submodule(${submodule_name}), rather than a single call toinitialize_submodules(). In either form, only submodules listed in .gitmodules will be updated. However, one could useupdate_submodule(${submodule_name})to update a submodule without cross-referencing .gitmodules.