Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change cmake submodule macros #17

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "vendor/googletest"]
[submodule "googletest"]
path = vendor/googletest
spahrenk marked this conversation as resolved.
Show resolved Hide resolved
url = https://github.com/google/googletest.git
[submodule "vendor/fmt"]
[submodule "fmt"]
path = vendor/fmt
url = https://github.com/fmtlib/fmt.git
94 changes: 78 additions & 16 deletions cmake/initialize-submodules.cmake
Original file line number Diff line number Diff line change
@@ -1,27 +1,89 @@
macro(initialize_submodules)
#update the named submodule

#attempt to add the submodule
#optional arguments:
# 1 - path
# 2 - alternate reference name
macro(add_submodule module_name)

message(STATUS "Attempting to add \"${module_name}\"")
#check of specific path is provided
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a remnant from debugging? I think we can remove all but maybe one or two of the STATUS messages. Either it was successful or it is a FATAL_ERROR.

set(MacroArgs ${ARGN})
list(LENGTH MacroArgs NumArgs)

set(have_submodule FALSE)
set(have_path FALSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to be used anywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used to count arguments. This has been rearranged.


# first optional argument is module path
if(NumArgs GREATER 0)
set(module_path ${ARGV1})
else()
set(module_path ${CMAKE_CURRENT_SOURCE_DIR}/${module_name})
endif()

# second optional argument is module reference
if(NumArgs GREATER 1)
set(module_ref_name ${ARGV2})
else()
set(module_ref_name ${module_name})
endif()

set(is_submodule FALSE)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
set(git_modules_file "${PROJECT_SOURCE_DIR}/.gitmodules")
if (EXISTS ${git_modules_file})
file(STRINGS ${git_modules_file} file_lines)
set(have_path FALSE)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already set to FALSE before.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but may be been changed within the loop

foreach(line ${file_lines})
if (${line} MATCHES "url =")
string(REGEX REPLACE "\\s*url = .*/(.*).git" "\\1" submodule "${line}")
string(STRIP "${submodule}" submodule)
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${submodule}")
message(FATAL_ERROR "Submodule directory \"${CMAKE_CURRENT_SOURCE_DIR}/${submodule}\" does not exist")
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.")
continue()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

endif()
# Initialize submodule if it hasn't already been cloned
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${submodule}/.git")
message(STATUS "Initialize ${submodule} submodule")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive "${CMAKE_CURRENT_SOURCE_DIR}/${submodule}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init --recursive ${CMAKE_CURRENT_SOURCE_DIR}/${submodule} failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()

if (is_submodule AND (NOT have_path))
string(FIND ${line} "path =" pos)
set(have_path NOT(pos EQUALS -1))
if (have_path)
string(REPLACE "path =" "" submodule_path ${line})
string(STRIP "${submodule_path}" submodule_path)
set(submodule_path "${PROJECT_SOURCE_DIR}/${submodule_path}")
continue()
endif()
endif()
endforeach()

if(is_submodule)
if(have_path)
message(STATUS "Updating submodule \"${module_name}\" at ${submodule_path}")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive "${submodule_path}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}\"")
else()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

message(FATAL_ERROR "Unable to update submodule \"${module_name}\"")
endif()
endif()
else()
message(STATUS "\"${module_name}\" is not a submodule of this project")
endif()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


endif()
endif()

if (TARGET ${module_name})
message(STATUS "Submodule \"${module_name}\" is a target.")
else()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

if(EXISTS "${module_path}")
message(STATUS "Adding subdirectory ${module_path} to this project")
add_subdirectory(${module_path})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

endif()
endif()
endmacro()

message("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give this version a try in a few situations and feel free to change the messaging however you prefer.


endmacro()
8 changes: 3 additions & 5 deletions vendor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
include(initialize-submodules)
initialize_submodules()

add_submodule(fmt)
if (NOT TARGET fmt)
add_subdirectory(fmt)
set(FMT_INSTALL OFF CACHE BOOL "" FORCE)
mark_as_advanced(FMT_CMAKE_DIR FMT_CUDA_TEST FMT_DEBUG_POSTFIX FMT_DOC FMT_FUZZ FMT_INC_DIR FMT_INSTALL FMT_INSTALL
FMT_LIB_DIR FMT_MODULE FMT_OS FMT_PEDANTIC FMT_PKGCONFIG_DIR FMT_SYSTEM_HEADERS FMT_TEST FMT_WERROR)
Expand All @@ -19,6 +18,5 @@ if (${PROJECT_NAME}_BUILD_TESTING AND NOT TARGET gtest)
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
mark_as_advanced(BUILD_GTEST BUILD_GMOCK INSTALL_GTEST)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/googletest)

endif ()
add_submodule(gtest ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
endif ()
Loading