[flutter_tools] Let CMake handle escaping (#70026)
This commit is contained in:
@@ -40,12 +40,12 @@ file(TO_CMAKE_PATH "$escapedProjectDir" PROJECT_DIR)
|
||||
|
||||
# Environment variables to pass to tool_backend.sh
|
||||
list(APPEND FLUTTER_TOOL_ENVIRONMENT
|
||||
"FLUTTER_ROOT=\\"$escapedFlutterRoot\\""
|
||||
"PROJECT_DIR=\\"$escapedProjectDir\\""
|
||||
"FLUTTER_ROOT=$escapedFlutterRoot"
|
||||
"PROJECT_DIR=$escapedProjectDir"
|
||||
''');
|
||||
for (final String key in environment.keys) {
|
||||
final String value = _escapeBackslashes(environment[key]);
|
||||
buffer.writeln(' "$key=\\"$value\\""');
|
||||
buffer.writeln(' "$key=$value"');
|
||||
}
|
||||
buffer.writeln(')');
|
||||
|
||||
|
||||
@@ -7,12 +7,14 @@ import '../base/analyze_size.dart';
|
||||
import '../base/common.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/logger.dart';
|
||||
import '../base/project_migrator.dart';
|
||||
import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
import '../cache.dart';
|
||||
import '../cmake.dart';
|
||||
import '../convert.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import '../migrations/cmake_custom_command_migration.dart';
|
||||
import '../plugins.dart';
|
||||
import '../project.dart';
|
||||
|
||||
@@ -35,6 +37,15 @@ Future<void> buildLinux(
|
||||
'to learn about adding Linux support to a project.');
|
||||
}
|
||||
|
||||
final List<ProjectMigrator> migrators = <ProjectMigrator>[
|
||||
CmakeCustomCommandMigration(linuxProject, globals.logger),
|
||||
];
|
||||
|
||||
final ProjectMigration migration = ProjectMigration(migrators);
|
||||
if (!migration.run()) {
|
||||
throwToolExit('Unable to migrate project files');
|
||||
}
|
||||
|
||||
// Build the environment that needs to be set for the re-entrant flutter build
|
||||
// step.
|
||||
final Map<String, String> environmentConfig = buildInfo.toEnvironmentConfig();
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
// 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 '../base/file_system.dart';
|
||||
import '../base/logger.dart';
|
||||
import '../base/project_migrator.dart';
|
||||
import '../project.dart';
|
||||
|
||||
// CMake's add_custom_command() should use VERBATIM to handle escaping of spaces
|
||||
// and special characters correctly.
|
||||
// See https://github.com/flutter/flutter/issues/67270.
|
||||
class CmakeCustomCommandMigration extends ProjectMigrator {
|
||||
CmakeCustomCommandMigration(CmakeBasedProject project, Logger logger)
|
||||
: _cmakeFile = project.managedCmakeFile,
|
||||
super(logger);
|
||||
|
||||
final File _cmakeFile;
|
||||
|
||||
@override
|
||||
bool migrate() {
|
||||
if (!_cmakeFile.existsSync()) {
|
||||
logger.printTrace('CMake project not found, skipping add_custom_command() VERBATIM migration');
|
||||
return true;
|
||||
}
|
||||
|
||||
final String originalProjectContents = _cmakeFile.readAsStringSync();
|
||||
// Example:
|
||||
//
|
||||
// add_custom_command(
|
||||
// OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS}
|
||||
// ${CMAKE_CURRENT_BINARY_DIR}/_phony_
|
||||
// COMMAND ${CMAKE_COMMAND} -E env
|
||||
// ${FLUTTER_TOOL_ENVIRONMENT}
|
||||
// "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh"
|
||||
// linux-x64 ${CMAKE_BUILD_TYPE}
|
||||
// )
|
||||
|
||||
// Match the whole add_custom_command() and append VERBATIM unless it
|
||||
// already exists.
|
||||
final RegExp addCustomCommand = RegExp(
|
||||
r'add_custom_command\(\s*(.*?)\s*\)',
|
||||
multiLine: true,
|
||||
dotAll: true,
|
||||
);
|
||||
|
||||
String newProjectContents = originalProjectContents;
|
||||
|
||||
final Iterable<RegExpMatch> matches = addCustomCommand.allMatches(originalProjectContents);
|
||||
|
||||
for (final RegExpMatch match in matches) {
|
||||
final String addCustomCommandOriginal = match.group(1);
|
||||
if (addCustomCommandOriginal?.contains('VERBATIM') == false) {
|
||||
final String addCustomCommandReplacement = '$addCustomCommandOriginal\n VERBATIM';
|
||||
newProjectContents = newProjectContents.replaceAll(addCustomCommandOriginal, addCustomCommandReplacement);
|
||||
}
|
||||
}
|
||||
if (originalProjectContents != newProjectContents) {
|
||||
logger.printStatus('add_custom_command() missing VERBATIM, updating.');
|
||||
_cmakeFile.writeAsStringSync(newProjectContents.toString());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -364,6 +364,9 @@ abstract class CmakeBasedProject {
|
||||
/// The native project CMake specification.
|
||||
File get cmakeFile;
|
||||
|
||||
/// Contains definitions for the Flutter library and the tool.
|
||||
File get managedCmakeFile;
|
||||
|
||||
/// Contains definitions for FLUTTER_ROOT, LOCAL_ENGINE, and more flags for
|
||||
/// the build.
|
||||
File get generatedCmakeConfigFile;
|
||||
@@ -1099,6 +1102,9 @@ class WindowsProject extends FlutterProjectPlatform implements CmakeBasedProject
|
||||
@override
|
||||
File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt');
|
||||
|
||||
@override
|
||||
File get managedCmakeFile => managedDirectory.childFile('CMakeLists.txt');
|
||||
|
||||
@override
|
||||
File get generatedCmakeConfigFile => ephemeralDirectory.childFile('generated_config.cmake');
|
||||
|
||||
@@ -1153,6 +1159,9 @@ class LinuxProject extends FlutterProjectPlatform implements CmakeBasedProject {
|
||||
@override
|
||||
File get cmakeFile => _editableDirectory.childFile('CMakeLists.txt');
|
||||
|
||||
@override
|
||||
File get managedCmakeFile => managedDirectory.childFile('CMakeLists.txt');
|
||||
|
||||
@override
|
||||
File get generatedCmakeConfigFile => ephemeralDirectory.childFile('generated_config.cmake');
|
||||
|
||||
|
||||
@@ -7,12 +7,14 @@ import '../base/analyze_size.dart';
|
||||
import '../base/common.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/logger.dart';
|
||||
import '../base/project_migrator.dart';
|
||||
import '../base/utils.dart';
|
||||
import '../build_info.dart';
|
||||
import '../cache.dart';
|
||||
import '../cmake.dart';
|
||||
import '../convert.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import '../migrations/cmake_custom_command_migration.dart';
|
||||
import '../plugins.dart';
|
||||
import '../project.dart';
|
||||
import 'visual_studio.dart';
|
||||
@@ -35,6 +37,15 @@ Future<void> buildWindows(WindowsProject windowsProject, BuildInfo buildInfo, {
|
||||
'to learn about adding Windows support to a project.');
|
||||
}
|
||||
|
||||
final List<ProjectMigrator> migrators = <ProjectMigrator>[
|
||||
CmakeCustomCommandMigration(windowsProject, globals.logger),
|
||||
];
|
||||
|
||||
final ProjectMigration migration = ProjectMigration(migrators);
|
||||
if (!migration.run()) {
|
||||
throwToolExit('Unable to migrate project files');
|
||||
}
|
||||
|
||||
// Ensure that necessary ephemeral files are generated and up to date.
|
||||
_writeGeneratedFlutterConfig(windowsProject, buildInfo, target);
|
||||
createPluginSymlinks(windowsProject.parent);
|
||||
|
||||
Reference in New Issue
Block a user