cmake.dart 2.01 KB
Newer Older
1 2 3 4
// 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.

5
import 'project.dart';
6

7
/// Extracts the `BINARY_NAME` from a project's CMake file.
8 9
///
/// Returns `null` if it cannot be found.
10
String getCmakeExecutableName(CmakeBasedProject project) {
11 12 13 14 15 16 17 18 19 20 21 22 23
  if (!project.cmakeFile.existsSync()) {
    return null;
  }
  final RegExp nameSetPattern = RegExp(r'^\s*set\(BINARY_NAME\s*"(.*)"\s*\)\s*$');
  for (final String line in project.cmakeFile.readAsLinesSync()) {
    final RegExpMatch match = nameSetPattern.firstMatch(line);
    if (match != null) {
      return match.group(1);
    }
  }
  return null;
}

24 25 26 27
String _escapeBackslashes(String s) {
  return s.replaceAll(r'\', r'\\');
}

28 29 30
/// Writes a generated CMake configuration file for [project], including
/// variables expected by the build template and an environment variable list
/// for calling back into Flutter.
31
void writeGeneratedCmakeConfig(String flutterRoot, CmakeBasedProject project, Map<String, String> environment) {
32 33
  // Only a limited set of variables are needed by the CMake files themselves,
  // the rest are put into a list to pass to the re-entrant build step.
34 35
  final String escapedFlutterRoot = _escapeBackslashes(flutterRoot);
  final String escapedProjectDir = _escapeBackslashes(project.parent.directory.path);
36 37
  final StringBuffer buffer = StringBuffer('''
# Generated code do not commit.
38 39
file(TO_CMAKE_PATH "$escapedFlutterRoot" FLUTTER_ROOT)
file(TO_CMAKE_PATH "$escapedProjectDir" PROJECT_DIR)
40 41 42

# Environment variables to pass to tool_backend.sh
list(APPEND FLUTTER_TOOL_ENVIRONMENT
43 44
  "FLUTTER_ROOT=\\"$escapedFlutterRoot\\""
  "PROJECT_DIR=\\"$escapedProjectDir\\""
45 46
''');
  for (final String key in environment.keys) {
47
    final String value = _escapeBackslashes(environment[key]);
48 49 50 51 52 53 54 55
    buffer.writeln('  "$key=\\"$value\\""');
  }
  buffer.writeln(')');

  project.generatedCmakeConfigFile
    ..createSync(recursive: true)
    ..writeAsStringSync(buffer.toString());
}