diff --git a/src/main.cpp b/src/main.cpp index 1e82a8b..525c737 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,6 +44,7 @@ int main(int argc, char** argv) argParser.AddArg(ArgDesc("-a", true, 1, "Add library projects, separated by ',' (ex: -a first,second,third)")); argParser.AddArg(ArgDesc("-x", true, 1, "Add executable submodule projects, separated by ',' (ex: -x first,second,third)")); argParser.AddArg(ArgDesc("-r", true, 0, "Remove the root project executable")); + argParser.AddArg(ArgDesc("-gs", true, 0, "Add git subsystem updating to the main cmake file.")); bool result = argParser.Parse(); @@ -223,7 +224,7 @@ int main(int argc, char** argv) ofs << "\nset(CMAKE_CXX_STANDARD " << cversion << ")"; ofs << "\nset(CMAKE_CXX_STANDARD_REQUIRED True)"; - ofs << "\n# setup target output directories"; + ofs << "\n\n# setup target output directories"; ofs << "\nset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)"; ofs << "\nset(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)"; ofs << "\nset(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)"; @@ -246,32 +247,61 @@ int main(int argc, char** argv) ofs << "\n\tPUBLIC src"; ofs << "\n)"; - // Link to any libs that exist - ofs << "\n\ntarget_link_directories(${PROJECT_NAME} "; - for (int i = 0; i < libraries.size(); i++) + if (argParser.ContainsOption("-gs")) { - ofs << "\n\tPRIVATE ../" << libraries[i].c_str(); + ofs << "\n\n# DOWNLOAD ALL SUBMODULES"; + ofs << "\nfind_package(Git QUIET)"; + ofs << "\nif(GIT_FOUND AND EXISTS \"${PROJECT_SOURCE_DIR}/.git\")"; + ofs << "\n# Update submodules as needed"; + ofs << "\n option(GIT_SUBMODULE \"Check submodules during build\" ON)"; + ofs << "\n if(GIT_SUBMODULE)"; + ofs << "\n message(STATUS \"Submodule update\")"; + ofs << "\n execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive"; + ofs << "\n WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}"; + ofs << "\n RESULT_VARIABLE GIT_SUBMOD_RESULT)"; + ofs << "\n if(NOT GIT_SUBMOD_RESULT EQUAL \"0\")"; + ofs << "\n message(FATAL_ERROR \"git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules\")"; + ofs << "\n endif()"; + ofs << "\n endif()"; + ofs << "\nendif()"; + ofs << "\n"; + ofs << "\n# CHECK THAT ALL SUBMODULES EXIST"; + ofs << "\n#if(NOT EXISTS \"${PROJECT_SOURCE_DIR}/external//CMakeLists.txt\")"; + ofs << "\n# message(FATAL_ERROR \" submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.\")"; + ofs << "\n#endif()"; } - ofs << ")"; - ofs << "\n\ntarget_link_libraries(${PROJECT_NAME}"; + // include the libraries and submodules + ofs << "\n\n# add subdirectories here"; for (int i = 0; i < libraries.size(); i++) { - ofs << " " << libraries[i].c_str(); + ofs << "\nadd_subdirectory(src/" << libraries[i].c_str() << ")"; } - ofs << ")"; - } - - // include the libraries and submodules - for (int i = 0; i < libraries.size(); i++) - { - ofs << "\nadd_subdirectory(src/" << libraries[i].c_str() << ")"; - } + for (int i = 0; i < submods.size(); i++) + { + ofs << "\nadd_subdirectory(src/" << submods[i].c_str() << ")"; + } - for (int i = 0; i < submods.size(); i++) - { - ofs << "\nadd_subdirectory(src/" << submods[i].c_str() << ")"; + // Link to any libs that exist or add space to link to submodules added through git + if (libraries.size() > 0 || argParser.ContainsOption("-gs")) + { + + ofs << "\n\ntarget_link_directories(${PROJECT_NAME} "; + for (int i = 0; i < libraries.size(); i++) + { + ofs << "\n\tPRIVATE ../" << libraries[i].c_str(); + } + ofs << ")"; + + ofs << "\n\ntarget_link_libraries(${PROJECT_NAME}"; + for (int i = 0; i < libraries.size(); i++) + { + ofs << " " << libraries[i].c_str(); + } + ofs << ")"; + } + } ofs.close();