property_sheet.dart 3.89 KB
Newer Older
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
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:xml/xml.dart' as xml;

/// A utility class for building property sheet (.props) files for use
/// with MSBuild/Visual Studio projects.
class PropertySheet {
  /// Creates a PropertySheet with the given properties.
  const PropertySheet({
    this.environmentVariables,
    this.includePaths,
    this.libraryDependencies,
  });

  /// Variables to make available both as build macros and as environment
  /// variables for script steps.
  final Map<String, String> environmentVariables;

  /// Directories to search for headers.
  final List<String> includePaths;

  /// Libraries to link against.
  final List<String> libraryDependencies;

  @override
  String toString() {
    // See https://docs.microsoft.com/en-us/cpp/build/reference/vcxproj-file-structure#property-sheet-layout

    final xml.XmlBuilder builder = xml.XmlBuilder();
    builder.processing('xml', 'version="1.0" encoding="utf-8"');
    builder.element('Project', nest: () {
      builder.attribute('ToolsVersion', '4.0');
      builder.attribute(
          'xmlns', 'http://schemas.microsoft.com/developer/msbuild/2003');

      builder.element('ImportGroup', nest: () {
        builder.attribute('Label', 'PropertySheets');
      });
      builder.element('PropertyGroup', nest: () {
        builder.attribute('Label', 'UserMacros');

        _addEnviromentVariableUserMacros(builder);
      });
      builder.element('PropertyGroup');
      builder.element('ItemDefinitionGroup', nest: () {
        _addIncludePaths(builder);
        _addLibraryDependencies(builder);
      });
      builder.element('ItemGroup', nest: () {
        _addEnvironmentVariableBuildMacros(builder);
      });
    });

    return builder.build().toXmlString(pretty: true, indent: '  ');
  }

  /// Adds directories to the header search path.
  ///
  /// Must be called within the context of the ItemDefinitionGroup.
  void _addIncludePaths(xml.XmlBuilder builder) {
    if (includePaths == null || includePaths.isEmpty) {
      return;
    }
    builder.element('ClCompile', nest: () {
      builder.element('AdditionalIncludeDirectories', nest: () {
        builder.text('${includePaths.join(';')};%(AdditionalIncludeDirectories)');
      });
    });
  }

  /// Adds libraries to the link step.
  ///
  /// Must be called within the context of the ItemDefinitionGroup.
  void _addLibraryDependencies(xml.XmlBuilder builder) {
    if (libraryDependencies == null || libraryDependencies.isEmpty) {
      return;
    }
    builder.element('Link', nest: () {
      builder.element('AdditionalDependencies', nest: () {
        builder.text('${libraryDependencies.join(';')};%(AdditionalDependencies)');
      });
    });
  }

  /// Writes key/value pairs for any environment variables as user macros.
  ///
  /// Must be called within the context of the UserMacros PropertyGroup.
  void _addEnviromentVariableUserMacros(xml.XmlBuilder builder) {
    if (environmentVariables == null) {
      return;
    }
    for (final MapEntry<String, String> variable in environmentVariables.entries) {
      builder.element(variable.key, nest: () {
        builder.text(variable.value);
      });
    }
  }

  /// Writes the BuildMacros to expose environment variable UserMacros to the
  /// environment.
  ///
  /// Must be called within the context of the ItemGroup.
  void _addEnvironmentVariableBuildMacros(xml.XmlBuilder builder) {
    if (environmentVariables == null) {
      return;
    }
    for (final String name in environmentVariables.keys) {
      builder.element('BuildMacro', nest: () {
        builder.attribute('Include', name);
        builder.element('Value', nest: () {
          builder.text('\$($name)');
        });
        builder.element('EnvironmentVariable', nest: () {
          builder.text('true');
        });
      });
    }
  }
}