summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorChad Elliott <elliottc@objectcomputing.com>2022-10-17 14:00:35 -0500
committerChad Elliott <elliottc@objectcomputing.com>2022-10-17 14:00:35 -0500
commitc27c961c628ee343b1279e925f73ac0f8d0d6751 (patch)
tree46ca75d17e3ce56cab9333f8f85218d97ef724bf /modules
parentcaca0a0e3bc9b6233d38cdb1b786edf113ff5d14 (diff)
downloadMPC-c27c961c628ee343b1279e925f73ac0f8d0d6751.tar.gz
Added a workspace of sorts to handle multiple projects.
Diffstat (limited to 'modules')
-rw-r--r--modules/CMakeWorkspaceCreator.pm80
1 files changed, 78 insertions, 2 deletions
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.";
}
}