Updates will be applied on October 27th between 12pm - 12:45pm EDT (UTC-0400). Gitlab may be slow during the maintenance window.

cpack.cxx 15.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

Andy Cedilnik's avatar
Andy Cedilnik committed
12
13
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14
15
16
17
18
19
20
21
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmSystemTools.h"

// Need these for documentation support.
#include "cmake.h"
#include "cmDocumentation.h"
Bill Hoffman's avatar
Bill Hoffman committed
22
#include "cmCPackGeneratorFactory.h"
Bill Hoffman's avatar
Bill Hoffman committed
23
#include "cmCPackGenerator.h"
24
25
26
27
#include "cmake.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
28

29
30
#include "cmCPackLog.h"

31
#include <cmsys/CommandLineArguments.hxx>
Bill Hoffman's avatar
Bill Hoffman committed
32
#include <memory> // auto_ptr
33
34

//----------------------------------------------------------------------------
35
static const char * cmDocumentationName[][3] =
36
{
37
38
39
  {0,
   "  cpack - Packaging driver provided by CMake.", 0},
  {0,0,0}
40
41
42
};

//----------------------------------------------------------------------------
43
static const char * cmDocumentationUsage[][3] =
44
{
45
  {0,
46
   "  cpack -G <generator> [options]",
47
48
   0},
  {0,0,0}
49
50
51
};

//----------------------------------------------------------------------------
52
static const char * cmDocumentationDescription[][3] =
53
{
54
  {0,
55
56
57
   "The \"cpack\" executable is the CMake packaging program.  "
   "CMake-generated build trees created for projects that use "
   "the INSTALL_* commands have packaging support.  "
58
   "This program will generate the package.", 0},
59
  CMAKE_STANDARD_INTRODUCTION,
60
  {0,0,0}
61
62
63
};

//----------------------------------------------------------------------------
64
static const char * cmDocumentationOptions[][3] =
65
66
{
    {"-G <generator>", "Use the specified generator to generate package.",
Andy Cedilnik's avatar
Andy Cedilnik committed
67
68
69
70
    "CPack may support multiple native packaging systems on certain "
      "platforms. A generator is responsible for generating input files for "
      "particular system and invoking that systems. Possible generator names "
      "are specified in the Generators section." },
71
    {"-C <Configuration>", "Specify the project configuration",
Andy Cedilnik's avatar
Andy Cedilnik committed
72
73
    "This option specifies the configuration that the project was build "
      "with, for example 'Debug', 'Release'." },
74
75
    {"-D <var>=<value>", "Set a CPack variable.", \
    "Set a variable that can be used by the generator."}, \
76
77
78
    {"--config <config file>", "Specify the config file.",
    "Specify the config file to use to create the package. By default "
      "CPackConfig.cmake in the current directory will be used." },
79
    {0,0,0}
80
81
82
};

//----------------------------------------------------------------------------
83
static const char * cmDocumentationSeeAlso[][3] =
84
{
85
86
87
    {0, "cmake", 0},
    {0, "ccmake", 0},
    {0, 0, 0}
88
89
90
91
92
93
94
95
96
};

//----------------------------------------------------------------------------
int cpackUnknownArgument(const char*, void*)
{
  return 1;
}

//----------------------------------------------------------------------------
97
98
99
struct cpackDefinitions
{
  typedef std::map<cmStdString, cmStdString> MapType;
Andy Cedilnik's avatar
Andy Cedilnik committed
100
101
  MapType Map;
  cmCPackLog *Log;
102
};
103
104

//----------------------------------------------------------------------------
Andy Cedilnik's avatar
Andy Cedilnik committed
105
int cpackDefinitionArgument(const char* argument, const char* cValue,
106
  void* call_data)
107
108
{
  (void)argument;
109
  cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
110
111
112
113
  std::string value = cValue;
  size_t pos = value.find_first_of("=");
  if ( pos == std::string::npos )
    {
Andy Cedilnik's avatar
Andy Cedilnik committed
114
    cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR,
115
      "Please specify CPack definitions as: KEY=VALUE" << std::endl);
116
117
118
119
    return 0;
    }
  std::string key = value.substr(0, pos);
  value = value.c_str() + pos + 1;
Andy Cedilnik's avatar
Andy Cedilnik committed
120
121
  def->Map[key] = value;
  cmCPack_Log(def->Log, cmCPackLog::LOG_DEBUG, "Set CPack variable: "
122
    << key.c_str() << " to \"" << value.c_str() << "\"" << std::endl);
123
124
125
126
127
128
129
  return 1;
}

//----------------------------------------------------------------------------
// this is CPack.
int main (int argc, char *argv[])
{
130
  cmSystemTools::FindExecutableDirectory(argv[0]);
131
132
133
134
  cmCPackLog log;
  log.SetErrorPrefix("CPack Error: ");
  log.SetWarningPrefix("CPack Warning: ");
  log.SetOutputPrefix("CPack: ");
135
  log.SetVerbosePrefix("CPack Verbose: ");
136

137
138
139
140
  cmSystemTools::EnableMSVCDebugHook();

  if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
    {
141
142
    cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
      "Current working directory cannot be established." << std::endl);
143
144
145
146
147
    }

  std::string generator;
  bool help = false;
  bool helpVersion = false;
148
149
  bool verbose = false;
  bool debug = false;
150
151
152
153
154
  std::string helpFull;
  std::string helpMAN;
  std::string helpHTML;

  std::string cpackProjectName;
155
156
  std::string cpackProjectDirectory
    = cmsys::SystemTools::GetCurrentWorkingDirectory();
157
158
159
160
  std::string cpackBuildConfig;
  std::string cpackProjectVersion;
  std::string cpackProjectPatch;
  std::string cpackProjectVendor;
161
162
163
  std::string cpackConfigFile;

  cpackDefinitions definitions;
Andy Cedilnik's avatar
Andy Cedilnik committed
164
  definitions.Log = &log;
165

166
  cpackConfigFile = "";
167
168
169
170
171
172
173

  cmDocumentation doc;
  cmsys::CommandLineArguments arg;
  arg.Initialize(argc, argv);
  typedef cmsys::CommandLineArguments argT;
  // Help arguments
  arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
Andy Cedilnik's avatar
Andy Cedilnik committed
174
175
176
177
  arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull,
    "CPack help");
  arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML,
    "CPack help");
178
179
180
  arg.AddArgument("--help-man", argT::SPACE_ARGUMENT, &helpMAN, "CPack help");
  arg.AddArgument("--version", argT::NO_ARGUMENT, &helpVersion, "CPack help");

181
182
183
  arg.AddArgument("-V", argT::NO_ARGUMENT, &verbose, "CPack verbose");
  arg.AddArgument("--verbose", argT::NO_ARGUMENT, &verbose, "-V");
  arg.AddArgument("--debug", argT::NO_ARGUMENT, &debug, "-V");
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  arg.AddArgument("--config", argT::SPACE_ARGUMENT, &cpackConfigFile,
    "CPack configuration file");
  arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig,
    "CPack build configuration");
  arg.AddArgument("-G", argT::SPACE_ARGUMENT,
    &generator, "CPack generator");
  arg.AddArgument("-P", argT::SPACE_ARGUMENT,
    &cpackProjectName, "CPack project name");
  arg.AddArgument("-R", argT::SPACE_ARGUMENT,
    &cpackProjectVersion, "CPack project version");
  arg.AddArgument("-B", argT::SPACE_ARGUMENT,
    &cpackProjectDirectory, "CPack project directory");
  arg.AddArgument("--patch", argT::SPACE_ARGUMENT,
    &cpackProjectPatch, "CPack project patch");
  arg.AddArgument("--vendor", argT::SPACE_ARGUMENT,
    &cpackProjectVendor, "CPack project vendor");
  arg.AddCallback("-D", argT::SPACE_ARGUMENT,
    cpackDefinitionArgument, &definitions, "CPack Definitions");
202
203
  arg.SetUnknownArgumentCallback(cpackUnknownArgument);

204
  // Parse command line
205
206
  int parsed = arg.Parse();

207
208
209
210
211
212
213
214
215
216
217
218
  // Setup logging
  if ( verbose )
    {
    log.SetVerbose(verbose);
    cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbse" << std::endl);
    }
  if ( debug )
    {
    log.SetDebug(debug);
    cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
    }

219
220
  cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
    "Read CPack config file: " << cpackConfigFile.c_str() << std::endl);
221

222
  cmake cminst;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
223
  cminst.RemoveUnscriptableCommands();
224
225
  cmGlobalGenerator cmgg;
  cmgg.SetCMakeInstance(&cminst);
Bill Hoffman's avatar
Bill Hoffman committed
226
  std::auto_ptr<cmLocalGenerator> cmlg(cmgg.CreateLocalGenerator());
227
  cmMakefile* globalMF = cmlg->GetMakefile();
228
229
230
231
232

  bool cpackConfigFileSpecified = true;
  if ( cpackConfigFile.empty() )
    {
    cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
233
    cpackConfigFile += "/CPackConfig.cmake";
234
235
    cpackConfigFileSpecified = false;
    }
236

Bill Hoffman's avatar
Bill Hoffman committed
237
  cmCPackGeneratorFactory generators;
238
  generators.SetLogger(&log);
Bill Hoffman's avatar
Bill Hoffman committed
239
  cmCPackGenerator* cpackGenerator = 0;
240

241
242
  if ( !helpFull.empty() || !helpMAN.empty() ||
    !helpHTML.empty() || helpVersion )
243
244
245
246
247
248
    {
    help = true;
    }

  if ( parsed && !help )
    {
Alexander Neundorf's avatar
   
Alexander Neundorf committed
249
250
    // find out which system cpack is running on, so it can setup the search
    // paths, so FIND_XXX() commands can be used in scripts
251
    cminst.AddCMakePaths();
Alexander Neundorf's avatar
   
Alexander Neundorf committed
252
    std::string systemFile = 
Bill Hoffman's avatar
Bill Hoffman committed
253
      globalMF->GetModulesFile("CMakeDetermineSystem.cmake");
Alexander Neundorf's avatar
   
Alexander Neundorf committed
254
255
256
257
258
259
260
    if (!globalMF->ReadListFile(0, systemFile.c_str()))
      {
      cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
        "Error reading CMakeDetermineSystem.cmake" << std::endl);
      return 1;
      }

Alexander Neundorf's avatar
   
Alexander Neundorf committed
261
    systemFile = 
Bill Hoffman's avatar
Bill Hoffman committed
262
      globalMF->GetModulesFile("CMakeSystemSpecificInformation.cmake");
Alexander Neundorf's avatar
   
Alexander Neundorf committed
263
264
265
266
267
268
269
    if (!globalMF->ReadListFile(0, systemFile.c_str()))
      {
      cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
        "Error reading CMakeSystemSpecificInformation.cmake" << std::endl);
      return 1;
      }

270
    if ( cmSystemTools::FileExists(cpackConfigFile.c_str()) )
271
      {
Ken Martin's avatar
Ken Martin committed
272
273
      cpackConfigFile = 
        cmSystemTools::CollapseFullPath(cpackConfigFile.c_str());
274
275
276
277
      cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
        "Read CPack configuration file: " << cpackConfigFile.c_str()
        << std::endl);
      if ( !globalMF->ReadListFile(0, cpackConfigFile.c_str()) )
278
        {
279
        cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
Alexander Neundorf's avatar
   
Alexander Neundorf committed
280
          "Problem reading CPack config file: \""
281
          << cpackConfigFile.c_str() << "\"" << std::endl);
282
283
        return 1;
        }
284
285
286
      }
    else if ( cpackConfigFileSpecified )
      {
287
288
289
      cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
        "Cannot find CPack config file: \"" << cpackConfigFile.c_str()
        << "\"" << std::endl);
290
291
292
      return 1;
      }

293
294
    if ( !generator.empty() )
      {
295
      globalMF->AddDefinition("CPACK_GENERATOR", generator.c_str());
296
297
298
      }
    if ( !cpackProjectName.empty() )
      {
299
      globalMF->AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
300
301
302
      }
    if ( !cpackProjectVersion.empty() )
      {
Andy Cedilnik's avatar
Andy Cedilnik committed
303
304
      globalMF->AddDefinition("CPACK_PACKAGE_VERSION",
        cpackProjectVersion.c_str());
305
306
307
      }
    if ( !cpackProjectVendor.empty() )
      {
Andy Cedilnik's avatar
Andy Cedilnik committed
308
309
      globalMF->AddDefinition("CPACK_PACKAGE_VENDOR",
        cpackProjectVendor.c_str());
310
311
312
      }
    if ( !cpackProjectDirectory.empty() )
      {
313
      globalMF->AddDefinition("CPACK_PACKAGE_DIRECTORY",
314
315
316
317
        cpackProjectDirectory.c_str());
      }
    if ( !cpackBuildConfig.empty() )
      {
318
      globalMF->AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
319
      }
320
    cpackDefinitions::MapType::iterator cdit;
Andy Cedilnik's avatar
Andy Cedilnik committed
321
322
    for ( cdit = definitions.Map.begin();
      cdit != definitions.Map.end();
323
      ++cdit )
324
      {
325
      globalMF->AddDefinition(cdit->first.c_str(), cdit->second.c_str());
326
327
      }

Brad King's avatar
Brad King committed
328
329
    const char* cpackModulesPath =
      globalMF->GetDefinition("CPACK_MODULE_PATH");
330
331
332
333
    if ( cpackModulesPath )
      {
      globalMF->AddDefinition("CMAKE_MODULE_PATH", cpackModulesPath);
      }
334
335
    const char* genList = globalMF->GetDefinition("CPACK_GENERATOR");
    if ( !genList )
336
      {
337
338
      cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
        "CPack generator not specified" << std::endl);
Andy Cedilnik's avatar
Andy Cedilnik committed
339
      parsed = 0;
340
      }
Andy Cedilnik's avatar
Andy Cedilnik committed
341
    else
342
      {
Andy Cedilnik's avatar
Andy Cedilnik committed
343
344
345
346
347
348
349
      std::vector<std::string> generatorsVector;
      cmSystemTools::ExpandListArgument(genList,
        generatorsVector);
      std::vector<std::string>::iterator it;
      for ( it = generatorsVector.begin();
        it != generatorsVector.end();
        ++it )
350
        {
Andy Cedilnik's avatar
Andy Cedilnik committed
351
352
353
        const char* gen = it->c_str();
        cmMakefile newMF(*globalMF);
        cmMakefile* mf = &newMF;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
354
355
356
357
358
359
360
361
362
363
364
365
        cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
          "Specified generator: " << gen << std::endl);
        if ( parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME") )
          {
          cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
            "CPack project name not specified" << std::endl);
          parsed = 0;
          }
        if ( parsed && !(mf->GetDefinition("CPACK_PACKAGE_VERSION")
            || mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
            mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR")
            && mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")) )
366
          {
Alexander Neundorf's avatar
   
Alexander Neundorf committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
          cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
            "CPack project version not specified" << std::endl
            << "Specify CPACK_PACKAGE_VERSION, or "
            "CPACK_PACKAGE_VERSION_MAJOR, "
            "CPACK_PACKAGE_VERSION_MINOR, and CPACK_PACKAGE_VERSION_PATCH."
            << std::endl);
          parsed = 0;
          }
        if ( parsed )
          {
          cpackGenerator = generators.NewGenerator(gen);
          if ( !cpackGenerator )
            {
            cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
              "Cannot initialize CPack generator: "
Alexander Neundorf's avatar
   
Alexander Neundorf committed
382
              << gen << std::endl);
Alexander Neundorf's avatar
   
Alexander Neundorf committed
383
384
            parsed = 0;
            }
385
          if ( parsed && !cpackGenerator->Initialize(gen, mf) )
386
387
            {
            cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
Alexander Neundorf's avatar
   
Alexander Neundorf committed
388
              "Cannot initialize the generator " << gen << std::endl);
389
390
            parsed = 0;
            }
Alexander Neundorf's avatar
   
Alexander Neundorf committed
391
392
393
394

          if ( !mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
            !mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
            !mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS") )
395
396
            {
            cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
Alexander Neundorf's avatar
   
Alexander Neundorf committed
397
398
399
400
              "Please specify build tree of the project that uses CMake "
              "using CPACK_INSTALL_CMAKE_PROJECTS, specify "
              "CPACK_INSTALL_COMMANDS, or specify "
              "CPACK_INSTALLED_DIRECTORIES."
Andy Cedilnik's avatar
Andy Cedilnik committed
401
              << std::endl);
402
403
            parsed = 0;
            }
Andy Cedilnik's avatar
Andy Cedilnik committed
404
          if ( parsed )
405
            {
Alexander Neundorf's avatar
   
Alexander Neundorf committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#ifdef _WIN32
            std::string comspec = "cmw9xcom.exe";
            cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
#endif

            const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
            cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Use generator: "
              << cpackGenerator->GetNameOfClass() << std::endl);
            cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "For project: "
              << projName << std::endl);

            const char* projVersion = 
              mf->GetDefinition("CPACK_PACKAGE_VERSION");
            if ( !projVersion )
Andy Cedilnik's avatar
Andy Cedilnik committed
420
              {
Alexander Neundorf's avatar
   
Alexander Neundorf committed
421
422
423
424
425
426
427
428
429
430
431
              const char* projVersionMajor
                = mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
              const char* projVersionMinor
                = mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
              const char* projVersionPatch
                = mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
              cmOStringStream ostr;
              ostr << projVersionMajor << "." << projVersionMinor << "."
                << projVersionPatch;
              mf->AddDefinition("CPACK_PACKAGE_VERSION", 
                                ostr.str().c_str());
Andy Cedilnik's avatar
Andy Cedilnik committed
432
433
              }

434
            int res = cpackGenerator->DoPackage();
Alexander Neundorf's avatar
   
Alexander Neundorf committed
435
            if ( !res )
Andy Cedilnik's avatar
Andy Cedilnik committed
436
437
              {
              cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
Alexander Neundorf's avatar
   
Alexander Neundorf committed
438
439
                "Error when generating package: " << projName << std::endl);
              return 1;
Andy Cedilnik's avatar
Andy Cedilnik committed
440
              }
441
442
            }
          }
443
        }
Andy Cedilnik's avatar
Andy Cedilnik committed
444
445
      }
    }
446

Alexander Neundorf's avatar
   
Alexander Neundorf committed
447
  if ( help )
Andy Cedilnik's avatar
Andy Cedilnik committed
448
449
450
451
    {
    doc.CheckOptions(argc, argv);
    // Construct and print requested documentation.
    doc.SetName("cpack");
452
453
454
455
    doc.SetSection("Name",cmDocumentationName);
    doc.SetSection("Usage",cmDocumentationUsage);
    doc.SetSection("Description",cmDocumentationDescription);
    doc.SetSection("Options",cmDocumentationOptions);
Andy Cedilnik's avatar
Andy Cedilnik committed
456
457

    std::vector<cmDocumentationEntry> v;
Bill Hoffman's avatar
Bill Hoffman committed
458
    cmCPackGeneratorFactory::DescriptionsMap::const_iterator generatorIt;
Andy Cedilnik's avatar
Andy Cedilnik committed
459
460
461
462
463
    for( generatorIt = generators.GetGeneratorsList().begin();
      generatorIt != generators.GetGeneratorsList().end();
      ++ generatorIt )
      {
      cmDocumentationEntry e;
464
465
466
      e.Name = generatorIt->first.c_str();
      e.Brief = generatorIt->second.c_str();
      e.Full = "";
Andy Cedilnik's avatar
Andy Cedilnik committed
467
468
      v.push_back(e);
      }
469
    doc.SetSection("Generators",v);
470

Andy Cedilnik's avatar
Andy Cedilnik committed
471
    doc.SetSeeAlsoList(cmDocumentationSeeAlso);
472
#undef cout
Andy Cedilnik's avatar
Andy Cedilnik committed
473
    return doc.PrintRequestedDocumentation(std::cout)? 0:1;
474
#define cout no_cout_use_cmCPack_Log
475
476
477
478
    }

  return 0;
}