summaryrefslogtreecommitdiff
path: root/modules/CMakeWorkspaceCreator.pm
blob: 6a9d994a8b03c8d3233144a660cc19ae06134ca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package CMakeWorkspaceCreator;

# ************************************************************
# Description   : A CMake Workspace creator
# Author        : Chad Elliott
# Create Date   : 10/10/2022
# ************************************************************

# ************************************************************
# Pragmas
# ************************************************************

use strict;
use File::Basename;

use CMakeProjectCreator;
use WorkspaceCreator;

use vars qw(@ISA);
@ISA = qw(WorkspaceCreator);

# ************************************************************
# Data Section
# ************************************************************

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();

  $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 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);

  ## 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 (@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;

      push(@project_dirs, $dir);
    }
  }

  ## Create the basis of a project so that we can add our add_subdirectory()
  ## calls below it.
  my $crlf = $self->crlf();
  my $ws = TemplateParser::actual_normalize(undef, $self->get_workspace_name());
  print $fh "cmake_minimum_required(VERSION $version)", $crlf,
            "project($ws CXX)", $crlf;

  my $first = 1;
  foreach my $dir (@project_dirs) {
    if ($first) {
      $first = undef;
      print $fh $crlf;
    }
    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;
      }
      print $fh "include($entry)$crlf";
    }
  }

  return $status, $errorString;
}

1;