diff options
-rw-r--r-- | modules/CMakeWorkspaceCreator.pm | 135 | ||||
-rw-r--r-- | templates/cmake.mpd | 32 |
2 files changed, 77 insertions, 90 deletions
diff --git a/modules/CMakeWorkspaceCreator.pm b/modules/CMakeWorkspaceCreator.pm index fc5c3a80..9cc0d0f4 100644 --- a/modules/CMakeWorkspaceCreator.pm +++ b/modules/CMakeWorkspaceCreator.pm @@ -29,6 +29,15 @@ my $version = '3.12.0'; # Subroutine Section # ************************************************************ +sub workspace_per_project { + #my $self = shift; + return 1; +} + +sub workspace_file_name { + return 'CMakeLists.txt'; +} + sub pre_workspace { my($self, $fh) = @_; my $crlf = $self->crlf(); @@ -42,95 +51,73 @@ sub pre_workspace { '# ', $self->create_command_line_string($0, @ARGV), $crlf); } -sub write_and_compare_file { - my($self, $outdir, $oname, $func, @params) = @_; +sub get_top_directory { + my($self, $path) = @_; + + ## Get the top level directory in the path. If the path does not contain + ## a directory, the path will be returned unmodified. + my $dir = $path; + my $done = 0; + do { + ## Go up one directory. If we were already at the top directory, + ## we're finished. + my $next = $self->mpc_dirname($dir); + if ($next eq '.') { + $done = 1; + } + else { + $dir = $next; + } + } while(!$done); + + return $dir; +} + +sub write_comps { + my($self, $fh, $creator) = @_; my $status = 1; my $errorString = ''; my @project_dirs; + my @projects = $self->sort_dependencies($self->get_projects(), 0); - ## Set the output directory if one wasn't provided - $outdir = $self->get_outdir() if (!defined $outdir); - - ## Remove the existing workspace, if one exists. - my $wsname = "$outdir/CMakeLists.txt"; - unlink($wsname); - - ## Rename the first (and hopefully the only) project in the directory to what - ## CMake expects. + ## Build a list of top level directories. We only want to go down one + ## directory. The workspace in that directory will handle going to + ## other subdirectories. my %dirs; - foreach my $entry ($self->sort_dependencies($self->get_projects(), 0)) { - my $dir = dirname($entry); - if (!exists $dirs{$dir}) { + foreach my $entry (@projects) { + my $dir = $self->get_top_directory($entry); + if ($dir ne $entry && !exists $dirs{$dir}) { ## Keep track of the project existing in this directory $dirs{$dir} = 1; - ## Rename the project file to CMakeLists.txt and if it fails, we need to - ## propagate that back to the caller. - if (rename($entry, "$dir/CMakeLists.txt") == 0) { - $status = 0; - $errorString = "Unable to rename $entry"; - last; - } - push(@project_dirs, $dir); } - else { - $self->warning("Multiple projects in the same directory are not " . - "supported: $dir"); - } } - if ($status) { - ## See if a project file exists in this directory. Since the workspace and - ## project files would have the same name, we need to read the contents of - ## the file and then insert add_subdirectory() calls to it. - my @lines; - my $insert; - my $fh = new FileHandle(); - if (open($fh, $wsname)) { - for(my $i = 0; <$fh>; $i++) { - push(@lines, $_); - if (/find_package\(/) { - ## We need to insert the calls to add_subdirectory() after the last - ## find_package() to allow for configuration settings from packages. - ## This will allow us to modify the CMakeLists.txt to possibly make - ## certain subdirectories conditional. - $insert = $i; - } - } - close($fh); - } - - if ($#lines == -1) { - ## If a project doesn't exist, create the basis of a project so that we - ## can add our add_subdirectory() calls below it. - push(@lines, "cmake_minimum_required(VERSION $version)" . $self->crlf(), - "project(workspace CXX)" . $self->crlf()); - $insert = $#lines; + ## Create the basis of a project so that we can add our add_subdirectory() + ## calls below it. + my $crlf = $self->crlf(); + print $fh "cmake_minimum_required(VERSION $version)", $crlf, + "project(workspace CXX)", $crlf; + + my $first = 1; + foreach my $dir (@project_dirs) { + if ($first) { + $first = undef; + print $fh $crlf; } + print $fh "add_subdirectory($dir)$crlf"; + } - ## Create the workspace here. - if (open($fh, ">$wsname")) { - ## Write out the pre-workspace information - $self->pre_workspace($fh); - - for(my $i = 0; $i <= $#lines; $i++) { - print $fh "$lines[$i]"; - if ($i == $insert) { - my $crlf = $self->crlf(); - print $fh $crlf; - foreach my $dir (@project_dirs) { - if ($dir ne $outdir) { - print $fh "add_subdirectory($dir)$crlf"; - } - } - } + $first = 1; + foreach my $entry (@projects) { + my $dir = $self->mpc_dirname($entry); + if ($dir eq '.') { + if ($first) { + $first = undef; + print $fh $crlf; } - close($fh); - } - else { - $status = 0; - $errorString = "Unable to open $wsname for output."; + print $fh "include($entry)$crlf"; } } diff --git a/templates/cmake.mpd b/templates/cmake.mpd index e7d95eee..f5fa46e8 100644 --- a/templates/cmake.mpd +++ b/templates/cmake.mpd @@ -11,15 +11,15 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" <%if(need_staticflags)%> find_package(<%package%> REQUIRED) <%endfor%> -set(SOURCE_FILES <%source_files%>) +set(SOURCE_FILES_<%uc(normalize(project_name))%> <%source_files%>) <%if(libs || lit_libs || pure_libs)%> if(CMAKE_CONFIGURATION_TYPES) -set(TARGET_LINK_LIBRARIES<%if(libs)%> <%foreach(configurations)%><%fornotfirst(" ")%>$<$<CONFIG:<%configuration%>>:<%foreach(libs)%><%fornotfirst(" ")%><%lib%>${LIBRARY_DECORATOR}<%lib_modifier%><%endfor%>><%fornotlast("\n")%><%endfor%><%endif%><%if(lit_libs)%> <%lit_libs%><%endif%><%if(pure_libs)%> <%pure_libs%><%endif%>) +set(TARGET_LINK_LIBRARIES_<%uc(normalize(project_name))%><%if(libs)%> <%foreach(configurations)%><%fornotfirst(" ")%>$<$<CONFIG:<%configuration%>>:<%foreach(libs)%><%fornotfirst(" ")%><%lib%>${LIBRARY_DECORATOR}<%lib_modifier%><%endfor%>><%fornotlast("\n")%><%endfor%><%endif%><%if(lit_libs)%> <%lit_libs%><%endif%><%if(pure_libs)%> <%pure_libs%><%endif%>) else() -set(TARGET_LINK_LIBRARIES<%if(libs)%><%foreach(libs)%> <%lib%>${LIBRARY_DECORATOR}<%endfor%><%endif%><%if(lit_libs)%> <%lit_libs%><%endif%><%if(pure_libs)%> <%pure_libs%><%endif%>) +set(TARGET_LINK_LIBRARIES_<%uc(normalize(project_name))%><%if(libs)%><%foreach(libs)%> <%lib%>${LIBRARY_DECORATOR}<%endfor%><%endif%><%if(lit_libs)%> <%lit_libs%><%endif%><%if(pure_libs)%> <%pure_libs%><%endif%>) endif() <%endif%> -set(PROJECT_TARGET <%if(exename)%><%exename%><%else%><%if(sharedname)%><%sharedname%>${LIBRARY_DECORATOR}<%else%><%if(staticname)%><%staticname%>${LIBRARY_DECORATOR}<%else%><%project_name%>${LIBRARY_DECORATOR}<%endif%><%endif%><%endif%>) +set(PROJECT_TARGET_<%uc(normalize(project_name))%> <%if(exename)%><%exename%><%else%><%if(sharedname)%><%sharedname%>${LIBRARY_DECORATOR}<%else%><%if(staticname)%><%staticname%>${LIBRARY_DECORATOR}<%else%><%project_name%>${LIBRARY_DECORATOR}<%endif%><%endif%><%endif%>) <%marker(macros)%> <%if(exeout)%> @@ -65,18 +65,18 @@ add_compile_options(<%compile_flags%>) <%endif%> <%if(exename)%> -add_executable(${PROJECT_TARGET} ${SOURCE_FILES}) -target_link_libraries(${PROJECT_TARGET} ${TARGET_LINK_LIBRARIES}) +add_executable(${PROJECT_TARGET_<%uc(normalize(project_name))%>} ${SOURCE_FILES_<%uc(normalize(project_name))%>}) +target_link_libraries(${PROJECT_TARGET_<%uc(normalize(project_name))%>} ${TARGET_LINK_LIBRARIES_<%uc(normalize(project_name))%>}) <%if(staticflags)%> if(NOT BUILD_SHARED_LIBS) add_compile_definitions(<%staticflags%>) - set_property(TARGET ${PROJECT_TARGET} PROPERTY + set_property(TARGET ${PROJECT_TARGET_<%uc(normalize(project_name))%>} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") endif() <%endif%> <%else%> <%if(sharedname)%> -add_library(${PROJECT_TARGET} ${SOURCE_FILES}) +add_library(${PROJECT_TARGET_<%uc(normalize(project_name))%>} ${SOURCE_FILES_<%uc(normalize(project_name))%>}) <%if(dynamicflags)%> if(BUILD_SHARED_LIBS) add_compile_definitions(<%dynamicflags%>) @@ -92,10 +92,10 @@ if(NOT BUILD_SHARED_LIBS) endif() <%endif%> <%endif%> -target_link_libraries(${PROJECT_TARGET} ${TARGET_LINK_LIBRARIES}) +target_link_libraries(${PROJECT_TARGET_<%uc(normalize(project_name))%>} ${TARGET_LINK_LIBRARIES_<%uc(normalize(project_name))%>}) <%else%> <%if(staticname)%> -add_library(${PROJECT_TARGET} ${SOURCE_FILES}) +add_library(${PROJECT_TARGET_<%uc(normalize(project_name))%>} ${SOURCE_FILES_<%uc(normalize(project_name))%>}) <%if(staticflags)%> add_compile_definitions(<%staticflags%>) <%endif%> @@ -104,22 +104,22 @@ add_compile_definitions(<%staticflags%>) <%if(use_lib_modifier)%> <%foreach(configurations)%> <%if(lib_modifier)%> -set_target_properties(${PROJECT_TARGET} PROPERTIES <%uc(configuration)%>_POSTFIX "<%lib_modifier%>") +set_target_properties(${PROJECT_TARGET_<%uc(normalize(project_name))%>} PROPERTIES <%uc(configuration)%>_POSTFIX "<%lib_modifier%>") <%endif%> <%endfor%> <%endif%> <%endif%> <%if(includes)%> -target_include_directories(${PROJECT_TARGET} PRIVATE <%env_includes%>) +target_include_directories(${PROJECT_TARGET_<%uc(normalize(project_name))%>} PRIVATE <%env_includes%>) <%endif%> <%if(libpaths)%> -target_link_directories(${PROJECT_TARGET} PRIVATE <%env_libpaths%>) +target_link_directories(${PROJECT_TARGET_<%uc(normalize(project_name))%>} PRIVATE <%env_libpaths%>) <%endif%> <%if(pch_header)%> -target_precompile_headers(${PROJECT_TARGET} PRIVATE <%pch_header%>) +target_precompile_headers(${PROJECT_TARGET_<%uc(normalize(project_name))%>} PRIVATE <%pch_header%>) <%endif%> <%if(pch_source)%> @@ -137,7 +137,7 @@ add_compile_definitions(<%macros%>) include(<%custom_type%> OPTIONAL) <%foreach(custom_type->input_files)%> <%uc(custom_type)%>_TARGET_SOURCES( - ${PROJECT_TARGET} PUBLIC <%custom_type->input_file%> + ${PROJECT_TARGET_<%uc(normalize(project_name))%>} PUBLIC <%custom_type->input_file%> <%uc(custom_type)%>_OPTIONS <%if(flag_overrides(custom_type->input_file, commandflags))%><%flag_overrides(custom_type->input_file, commandflags)%><%else%><%custom_type->commandflags%><%endif%><%if(custom_type->output_option)%> <%custom_type->output_option%> <%if(flag_overrides(custom_type->input_file, gendir))%><%flag_overrides(custom_type->input_file, gendir)%>/<%basename(custom_type->input_file->output_file)%><%else%><%custom_type->input_file->output_file%><%endif%><%endif%>) <%endfor%> @@ -148,7 +148,7 @@ include(<%custom_type%> OPTIONAL) include(<%custom_type%> OPTIONAL) <%endif%> <%uc(custom_type)%>_TARGET_SOURCES( - ${PROJECT_TARGET} PUBLIC <%custom_type->input_file%> + ${PROJECT_TARGET_<%uc(normalize(project_name))%>} PUBLIC <%custom_type->input_file%> <%foreach(custom_type->input_file->commands)%> <%uc(custom_type->input_file->command->type)%>_OPTIONS <%custom_type->input_file->command->flags%><%fornotlast("\n")%><%endfor%>) |