From c27c961c628ee343b1279e925f73ac0f8d0d6751 Mon Sep 17 00:00:00 2001 From: Chad Elliott Date: Mon, 17 Oct 2022 14:00:35 -0500 Subject: Added a workspace of sorts to handle multiple projects. --- modules/CMakeWorkspaceCreator.pm | 80 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) (limited to 'modules') diff --git a/modules/CMakeWorkspaceCreator.pm b/modules/CMakeWorkspaceCreator.pm index 6f0aa106..4f173ca4 100644 --- a/modules/CMakeWorkspaceCreator.pm +++ b/modules/CMakeWorkspaceCreator.pm @@ -23,15 +23,36 @@ use vars qw(@ISA); # Subroutine Section # ************************************************************ +sub pre_workspace { + my($self, $fh) = @_; + my $crlf = $self->crlf(); + + $self->print_workspace_comment($fh, + '# CMake Workspace', $crlf, + '#', $crlf, + '# This file was generated by MPC.', $crlf, + '#', $crlf, + '# MPC Command:', $crlf, + '# ', $self->create_command_line_string($0, @ARGV), $crlf); +} + sub write_and_compare_file { my($self, $outdir, $oname, $func, @params) = @_; my $status = 1; my $errorString = ''; + my @project_dirs; + + ## 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. my %dirs; - foreach my $entry (@{$self->get_projects()}) { + foreach my $entry ($self->sort_dependencies($self->get_projects(), 0)) { my $dir = dirname($entry); if (!exists $dirs{$dir}) { ## Keep track of the project existing in this directory @@ -44,9 +65,64 @@ sub write_and_compare_file { $errorString = "Unable to rename $entry"; last; } + + push(@project_dirs, $dir); + } + else { + $self->warning("Multiple projects in the same workspace 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 $version = '3.12.0'; + my $fh = new FileHandle(); + if (open($fh, $wsname)) { + for(my $i = 0; <$fh>; $i++) { + push(@lines, $_); + if (/cmake_minimum_required\(VERSION ([^\)]+)\)/) { + $insert = $i; + $version = $1; + } + } + 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)" . $self->crlf()); + $insert = $#lines; + } + + ## 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"; + } + } + } + } + close($fh); } else { - $self->warning("Multiple projects in the same workspace are not supported"); + $status = 0; + $errorString = "Unable to open $wsname for output."; } } -- cgit v1.2.1