Skip to content

Conversation

@dnfield
Copy link
Contributor

@dnfield dnfield commented Oct 22, 2018

Currently, the tooling and build system for Xcode set some variables to control the Flutter build mode (debug/profile/release) based on the command line options in a generated file (Generated.xcconfig).

This file should not be edited, but it can easily fall out of sync with Add2App scenarios (or if the user is just trying to build and run Runner.xcodeproj directly in Xcode). It can cause several problems (such as inconsistency between Xcode build configuration and Flutter build mode, which local engine is used, or which engine architecture is used). The engine architecture issue in particular (incorrect $FLUTTER_FRAMEWORKS_DIR set from previous run that isn't updated by Xcode) will cause runtime errors that are confusing ("Engine could not start with configuration...").

This patch does the following:

  1. Remove logic to set $FLUTTER_BUILD_MODE and $FLUTTER_FRAMEWORKS_DIR in Generated.xcconfig.
  2. Add a "Profile" Build Configuration to our default Xcode project templates.
  3. Associate the "Profile" build configuration with "Profiling" build (e.g. ⌘I). (Fixes Build in profile mode when built by running Xcode's profiler  #15249)
  4. Refactor xcode_backend.sh to drive the $FLUTTER_BUILD_MODE from $CONFIGURATION, unless $FLUTTER_BUILD_MODE is explicitly set (for users with exotic/custom configurations).
  5. Ensure that if $LOCAL_ENGINE is set, it's compatible with the build mode (otherwise error out).
  6. Ensure that if you are trying to run in Profile or Release mode from Xcode on a Simulator, an error will be printed and the build will fail (this already fails when doing flutter run --profile, which is not invoked in Add2App scenarios).

@dnfield
Copy link
Contributor Author

dnfield commented Oct 23, 2018

/cc @matthew-carroll - I don't know that the code will be of much interest, but the basic problems may exist on the Android side as well (inconsistency between flutter run ... and how the module chooses to embed the engine library etc.).

@dnfield
Copy link
Contributor Author

dnfield commented Oct 23, 2018

This is a more comprehensive solution for what #23309 was trying to solve for (in part).

@dnfield
Copy link
Contributor Author

dnfield commented Oct 23, 2018

EchoError "ERROR: Flutter archive builds must be run in Release mode."
EchoError ""
EchoError "To correct, run:"
EchoError "To correct, ensure FLUTTER_BUILD_MODE to release or run:"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ensure"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh maybe you meant ensure .. is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ensure FLUTTER_BUILD_MODE is set to release... are you saying it should use a verb other than ensure?

printError(' https://stackoverflow.com/questions/19842746/adding-a-build-configuration-in-xcode');
printError(' If you have created a completely custom set of build configurations,');
printError(' you can set the FLUTTER_BUILD_MODE=${buildInfo.modeName.toLowerCase()}');
printError(' in the .xcconfig file for that configuration and run from Xcode.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indentation is a bit weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to avoid a wall of text in the warning - perhaps switching from hanging indent to regular indent would do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to add a couple extra new-lines and removed the extra indendtation.

}

static String _baseConfigurationFor(BuildInfo buildInfo) => buildInfo.isDebug ? 'Debug' : 'Release';
static String _baseConfigurationFor(BuildInfo buildInfo) => buildInfo.isDebug
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extraneous space after =>

but also, generally we prefer to not line-break a => expression; this might be clearer as a block with if statements and returns.

@Hixie
Copy link
Contributor

Hixie commented Oct 23, 2018

tests?

# Use FLUTTER_BUILD_MODE if it's set, otherwise use the Xcode build configuration name
# This means that if someone wants to use an Xcode build config other than Debug/Profile/Release,
# they _must_ set FLUTTER_BUILD_MODE so we know what type of artifact to build.
local build_mode="$(echo "${FLUTTER_BUILD_MODE:-${CONFIGURATION}}" | tr "[:upper:]" "[:lower:]")"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems really implicit and would cause issues with flavors https://medium.com/@salvatoregiordanoo/flavoring-flutter-392aaa875f36.

While you're here, I think it would be great if the flavors mechanism could be documented somewhere in https://flutter.io/docs/ if priorities could work. (So you can append to that doc the fact that you if create custom schemes / build configs, a $FLUTTER_BUILD_MODE variable needs to be defined or passed in)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flavors would already have issues with the current system, but they'd be hidden by the fact that you'd be left with whatever $FLUTTER_BUILD_MODE was set from the last time you ran from the command line. I agree this needs to be documented, but I'm not sure if we can really do much more to help flavors.

@dnfield
Copy link
Contributor Author

dnfield commented Oct 23, 2018

I've updated some existing tests to cover these scenarios.

I was considering writing some tests that might exercise xcode_backend.sh with different environment variable configurations to ensure it would fail when it should. I started writing a shell script to do it but that felt messy and not well integrated with our testing framework. I guess I could write something that would shell out to it from Dart with various environment configurations, unless there's a better idea.

@dnfield
Copy link
Contributor Author

dnfield commented Oct 23, 2018

Added some tests to test for early exits from the bash script.

Add2App in general needs some integration tests.

Copy link
Member

@xster xster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

EchoError "ERROR: Requested build with Flutter local engine at '${LOCAL_ENGINE}'"
EchoError "This engine is not compatible with FLUTTER_BUILD_MODE: '${build_mode}'."
EchoError "You can fix this by updating the LOCAL_ENGINE environment variable, or"
EchoError "by running flutter build ios --local-engine=..."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be more specific with the solution. e.g. you need to run --local-engine=ios_profile_unopt or some such if you use build_mode profile.

printError(' you can set the FLUTTER_BUILD_MODE=${buildInfo.modeName.toLowerCase()}');
printError(' in the .xcconfig file for that configuration and run from Xcode.');
printError('');
printError('4. Name the newly created configuration ${buildInfo.modeName}.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caveat that this step isn't necessary if you followed the 'If you have created a completely custom set of build configurations' part of the previous step.

@dnfield dnfield removed the request for review from chinmaygarde October 24, 2018 19:47
@dnfield dnfield merged commit def1d80 into flutter:master Oct 25, 2018
dnfield added a commit that referenced this pull request Oct 25, 2018
dnfield added a commit that referenced this pull request Oct 25, 2018
Xavjer pushed a commit to Xavjer/flutter that referenced this pull request Nov 1, 2018
* Use Xcode build configurations to drive Flutter build mode
Xavjer pushed a commit to Xavjer/flutter that referenced this pull request Nov 1, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build in profile mode when built by running Xcode's profiler

4 participants