| Conditions | 119 |
| Total Lines | 698 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like GenerateOutput() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Copyright (c) 2012 Google Inc. All rights reserved. |
||
| 603 | def GenerateOutput(target_list, target_dicts, data, params): |
||
| 604 | # Optionally configure each spec to use ninja as the external builder. |
||
| 605 | ninja_wrapper = params.get('flavor') == 'ninja' |
||
| 606 | if ninja_wrapper: |
||
| 607 | (target_list, target_dicts, data) = \ |
||
| 608 | gyp.xcode_ninja.CreateWrapper(target_list, target_dicts, data, params) |
||
| 609 | |||
| 610 | options = params['options'] |
||
| 611 | generator_flags = params.get('generator_flags', {}) |
||
| 612 | parallel_builds = generator_flags.get('xcode_parallel_builds', True) |
||
| 613 | serialize_all_tests = \ |
||
| 614 | generator_flags.get('xcode_serialize_all_test_runs', True) |
||
| 615 | upgrade_check_project_version = \ |
||
| 616 | generator_flags.get('xcode_upgrade_check_project_version', None) |
||
| 617 | |||
| 618 | # Format upgrade_check_project_version with leading zeros as needed. |
||
| 619 | if upgrade_check_project_version: |
||
| 620 | upgrade_check_project_version = str(upgrade_check_project_version) |
||
| 621 | while len(upgrade_check_project_version) < 4: |
||
| 622 | upgrade_check_project_version = '0' + upgrade_check_project_version |
||
| 623 | |||
| 624 | skip_excluded_files = \ |
||
| 625 | not generator_flags.get('xcode_list_excluded_files', True) |
||
| 626 | xcode_projects = {} |
||
| 627 | for build_file, build_file_dict in data.iteritems(): |
||
| 628 | (build_file_root, build_file_ext) = os.path.splitext(build_file) |
||
| 629 | if build_file_ext != '.gyp': |
||
| 630 | continue |
||
| 631 | xcodeproj_path = build_file_root + options.suffix + '.xcodeproj' |
||
| 632 | if options.generator_output: |
||
| 633 | xcodeproj_path = os.path.join(options.generator_output, xcodeproj_path) |
||
| 634 | xcp = XcodeProject(build_file, xcodeproj_path, build_file_dict) |
||
| 635 | xcode_projects[build_file] = xcp |
||
| 636 | pbxp = xcp.project |
||
| 637 | |||
| 638 | # Set project-level attributes from multiple options |
||
| 639 | project_attributes = {}; |
||
| 640 | if parallel_builds: |
||
| 641 | project_attributes['BuildIndependentTargetsInParallel'] = 'YES' |
||
| 642 | if upgrade_check_project_version: |
||
| 643 | project_attributes['LastUpgradeCheck'] = upgrade_check_project_version |
||
| 644 | project_attributes['LastTestingUpgradeCheck'] = \ |
||
| 645 | upgrade_check_project_version |
||
| 646 | project_attributes['LastSwiftUpdateCheck'] = \ |
||
| 647 | upgrade_check_project_version |
||
| 648 | pbxp.SetProperty('attributes', project_attributes) |
||
| 649 | |||
| 650 | # Add gyp/gypi files to project |
||
| 651 | if not generator_flags.get('standalone'): |
||
| 652 | main_group = pbxp.GetProperty('mainGroup') |
||
| 653 | build_group = gyp.xcodeproj_file.PBXGroup({'name': 'Build'}) |
||
| 654 | main_group.AppendChild(build_group) |
||
| 655 | for included_file in build_file_dict['included_files']: |
||
| 656 | build_group.AddOrGetFileByPath(included_file, False) |
||
| 657 | |||
| 658 | xcode_targets = {} |
||
| 659 | xcode_target_to_target_dict = {} |
||
| 660 | for qualified_target in target_list: |
||
| 661 | [build_file, target_name, toolset] = \ |
||
| 662 | gyp.common.ParseQualifiedTarget(qualified_target) |
||
| 663 | |||
| 664 | spec = target_dicts[qualified_target] |
||
| 665 | if spec['toolset'] != 'target': |
||
| 666 | raise Exception( |
||
| 667 | 'Multiple toolsets not supported in xcode build (target %s)' % |
||
| 668 | qualified_target) |
||
| 669 | configuration_names = [spec['default_configuration']] |
||
| 670 | for configuration_name in sorted(spec['configurations'].keys()): |
||
| 671 | if configuration_name not in configuration_names: |
||
| 672 | configuration_names.append(configuration_name) |
||
| 673 | xcp = xcode_projects[build_file] |
||
| 674 | pbxp = xcp.project |
||
| 675 | |||
| 676 | # Set up the configurations for the target according to the list of names |
||
| 677 | # supplied. |
||
| 678 | xccl = CreateXCConfigurationList(configuration_names) |
||
| 679 | |||
| 680 | # Create an XCTarget subclass object for the target. The type with |
||
| 681 | # "+bundle" appended will be used if the target has "mac_bundle" set. |
||
| 682 | # loadable_modules not in a mac_bundle are mapped to |
||
| 683 | # com.googlecode.gyp.xcode.bundle, a pseudo-type that xcode.py interprets |
||
| 684 | # to create a single-file mh_bundle. |
||
| 685 | _types = { |
||
| 686 | 'executable': 'com.apple.product-type.tool', |
||
| 687 | 'loadable_module': 'com.googlecode.gyp.xcode.bundle', |
||
| 688 | 'shared_library': 'com.apple.product-type.library.dynamic', |
||
| 689 | 'static_library': 'com.apple.product-type.library.static', |
||
| 690 | 'mac_kernel_extension': 'com.apple.product-type.kernel-extension', |
||
| 691 | 'executable+bundle': 'com.apple.product-type.application', |
||
| 692 | 'loadable_module+bundle': 'com.apple.product-type.bundle', |
||
| 693 | 'loadable_module+xctest': 'com.apple.product-type.bundle.unit-test', |
||
| 694 | 'shared_library+bundle': 'com.apple.product-type.framework', |
||
| 695 | 'executable+extension+bundle': 'com.apple.product-type.app-extension', |
||
| 696 | 'executable+watch+extension+bundle': |
||
| 697 | 'com.apple.product-type.watchkit-extension', |
||
| 698 | 'executable+watch+bundle': |
||
| 699 | 'com.apple.product-type.application.watchapp', |
||
| 700 | 'mac_kernel_extension+bundle': 'com.apple.product-type.kernel-extension', |
||
| 701 | } |
||
| 702 | |||
| 703 | target_properties = { |
||
| 704 | 'buildConfigurationList': xccl, |
||
| 705 | 'name': target_name, |
||
| 706 | } |
||
| 707 | |||
| 708 | type = spec['type'] |
||
| 709 | is_xctest = int(spec.get('mac_xctest_bundle', 0)) |
||
| 710 | is_bundle = int(spec.get('mac_bundle', 0)) or is_xctest |
||
| 711 | is_app_extension = int(spec.get('ios_app_extension', 0)) |
||
| 712 | is_watchkit_extension = int(spec.get('ios_watchkit_extension', 0)) |
||
| 713 | is_watch_app = int(spec.get('ios_watch_app', 0)) |
||
| 714 | if type != 'none': |
||
| 715 | type_bundle_key = type |
||
| 716 | if is_xctest: |
||
| 717 | type_bundle_key += '+xctest' |
||
| 718 | assert type == 'loadable_module', ( |
||
| 719 | 'mac_xctest_bundle targets must have type loadable_module ' |
||
| 720 | '(target %s)' % target_name) |
||
| 721 | elif is_app_extension: |
||
| 722 | assert is_bundle, ('ios_app_extension flag requires mac_bundle ' |
||
| 723 | '(target %s)' % target_name) |
||
| 724 | type_bundle_key += '+extension+bundle' |
||
| 725 | elif is_watchkit_extension: |
||
| 726 | assert is_bundle, ('ios_watchkit_extension flag requires mac_bundle ' |
||
| 727 | '(target %s)' % target_name) |
||
| 728 | type_bundle_key += '+watch+extension+bundle' |
||
| 729 | elif is_watch_app: |
||
| 730 | assert is_bundle, ('ios_watch_app flag requires mac_bundle ' |
||
| 731 | '(target %s)' % target_name) |
||
| 732 | type_bundle_key += '+watch+bundle' |
||
| 733 | elif is_bundle: |
||
| 734 | type_bundle_key += '+bundle' |
||
| 735 | |||
| 736 | xctarget_type = gyp.xcodeproj_file.PBXNativeTarget |
||
| 737 | try: |
||
| 738 | target_properties['productType'] = _types[type_bundle_key] |
||
| 739 | except KeyError, e: |
||
| 740 | gyp.common.ExceptionAppend(e, "-- unknown product type while " |
||
| 741 | "writing target %s" % target_name) |
||
| 742 | raise |
||
| 743 | else: |
||
| 744 | xctarget_type = gyp.xcodeproj_file.PBXAggregateTarget |
||
| 745 | assert not is_bundle, ( |
||
| 746 | 'mac_bundle targets cannot have type none (target "%s")' % |
||
| 747 | target_name) |
||
| 748 | assert not is_xctest, ( |
||
| 749 | 'mac_xctest_bundle targets cannot have type none (target "%s")' % |
||
| 750 | target_name) |
||
| 751 | |||
| 752 | target_product_name = spec.get('product_name') |
||
| 753 | if target_product_name is not None: |
||
| 754 | target_properties['productName'] = target_product_name |
||
| 755 | |||
| 756 | xct = xctarget_type(target_properties, parent=pbxp, |
||
| 757 | force_outdir=spec.get('product_dir'), |
||
| 758 | force_prefix=spec.get('product_prefix'), |
||
| 759 | force_extension=spec.get('product_extension')) |
||
| 760 | pbxp.AppendProperty('targets', xct) |
||
| 761 | xcode_targets[qualified_target] = xct |
||
| 762 | xcode_target_to_target_dict[xct] = spec |
||
| 763 | |||
| 764 | spec_actions = spec.get('actions', []) |
||
| 765 | spec_rules = spec.get('rules', []) |
||
| 766 | |||
| 767 | # Xcode has some "issues" with checking dependencies for the "Compile |
||
| 768 | # sources" step with any source files/headers generated by actions/rules. |
||
| 769 | # To work around this, if a target is building anything directly (not |
||
| 770 | # type "none"), then a second target is used to run the GYP actions/rules |
||
| 771 | # and is made a dependency of this target. This way the work is done |
||
| 772 | # before the dependency checks for what should be recompiled. |
||
| 773 | support_xct = None |
||
| 774 | # The Xcode "issues" don't affect xcode-ninja builds, since the dependency |
||
| 775 | # logic all happens in ninja. Don't bother creating the extra targets in |
||
| 776 | # that case. |
||
| 777 | if type != 'none' and (spec_actions or spec_rules) and not ninja_wrapper: |
||
| 778 | support_xccl = CreateXCConfigurationList(configuration_names); |
||
| 779 | support_target_suffix = generator_flags.get( |
||
| 780 | 'support_target_suffix', ' Support') |
||
| 781 | support_target_properties = { |
||
| 782 | 'buildConfigurationList': support_xccl, |
||
| 783 | 'name': target_name + support_target_suffix, |
||
| 784 | } |
||
| 785 | if target_product_name: |
||
| 786 | support_target_properties['productName'] = \ |
||
| 787 | target_product_name + ' Support' |
||
| 788 | support_xct = \ |
||
| 789 | gyp.xcodeproj_file.PBXAggregateTarget(support_target_properties, |
||
| 790 | parent=pbxp) |
||
| 791 | pbxp.AppendProperty('targets', support_xct) |
||
| 792 | xct.AddDependency(support_xct) |
||
| 793 | # Hang the support target off the main target so it can be tested/found |
||
| 794 | # by the generator during Finalize. |
||
| 795 | xct.support_target = support_xct |
||
| 796 | |||
| 797 | prebuild_index = 0 |
||
| 798 | |||
| 799 | # Add custom shell script phases for "actions" sections. |
||
| 800 | for action in spec_actions: |
||
| 801 | # There's no need to write anything into the script to ensure that the |
||
| 802 | # output directories already exist, because Xcode will look at the |
||
| 803 | # declared outputs and automatically ensure that they exist for us. |
||
| 804 | |||
| 805 | # Do we have a message to print when this action runs? |
||
| 806 | message = action.get('message') |
||
| 807 | if message: |
||
| 808 | message = 'echo note: ' + gyp.common.EncodePOSIXShellArgument(message) |
||
| 809 | else: |
||
| 810 | message = '' |
||
| 811 | |||
| 812 | # Turn the list into a string that can be passed to a shell. |
||
| 813 | action_string = gyp.common.EncodePOSIXShellList(action['action']) |
||
| 814 | |||
| 815 | # Convert Xcode-type variable references to sh-compatible environment |
||
| 816 | # variable references. |
||
| 817 | message_sh = gyp.xcodeproj_file.ConvertVariablesToShellSyntax(message) |
||
| 818 | action_string_sh = gyp.xcodeproj_file.ConvertVariablesToShellSyntax( |
||
| 819 | action_string) |
||
| 820 | |||
| 821 | script = '' |
||
| 822 | # Include the optional message |
||
| 823 | if message_sh: |
||
| 824 | script += message_sh + '\n' |
||
| 825 | # Be sure the script runs in exec, and that if exec fails, the script |
||
| 826 | # exits signalling an error. |
||
| 827 | script += 'exec ' + action_string_sh + '\nexit 1\n' |
||
| 828 | ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({ |
||
| 829 | 'inputPaths': action['inputs'], |
||
| 830 | 'name': 'Action "' + action['action_name'] + '"', |
||
| 831 | 'outputPaths': action['outputs'], |
||
| 832 | 'shellScript': script, |
||
| 833 | 'showEnvVarsInLog': 0, |
||
| 834 | }) |
||
| 835 | |||
| 836 | if support_xct: |
||
| 837 | support_xct.AppendProperty('buildPhases', ssbp) |
||
| 838 | else: |
||
| 839 | # TODO(mark): this assumes too much knowledge of the internals of |
||
| 840 | # xcodeproj_file; some of these smarts should move into xcodeproj_file |
||
| 841 | # itself. |
||
| 842 | xct._properties['buildPhases'].insert(prebuild_index, ssbp) |
||
| 843 | prebuild_index = prebuild_index + 1 |
||
| 844 | |||
| 845 | # TODO(mark): Should verify that at most one of these is specified. |
||
| 846 | if int(action.get('process_outputs_as_sources', False)): |
||
| 847 | for output in action['outputs']: |
||
| 848 | AddSourceToTarget(output, type, pbxp, xct) |
||
| 849 | |||
| 850 | if int(action.get('process_outputs_as_mac_bundle_resources', False)): |
||
| 851 | for output in action['outputs']: |
||
| 852 | AddResourceToTarget(output, pbxp, xct) |
||
| 853 | |||
| 854 | # tgt_mac_bundle_resources holds the list of bundle resources so |
||
| 855 | # the rule processing can check against it. |
||
| 856 | if is_bundle: |
||
| 857 | tgt_mac_bundle_resources = spec.get('mac_bundle_resources', []) |
||
| 858 | else: |
||
| 859 | tgt_mac_bundle_resources = [] |
||
| 860 | |||
| 861 | # Add custom shell script phases driving "make" for "rules" sections. |
||
| 862 | # |
||
| 863 | # Xcode's built-in rule support is almost powerful enough to use directly, |
||
| 864 | # but there are a few significant deficiencies that render them unusable. |
||
| 865 | # There are workarounds for some of its inadequacies, but in aggregate, |
||
| 866 | # the workarounds added complexity to the generator, and some workarounds |
||
| 867 | # actually require input files to be crafted more carefully than I'd like. |
||
| 868 | # Consequently, until Xcode rules are made more capable, "rules" input |
||
| 869 | # sections will be handled in Xcode output by shell script build phases |
||
| 870 | # performed prior to the compilation phase. |
||
| 871 | # |
||
| 872 | # The following problems with Xcode rules were found. The numbers are |
||
| 873 | # Apple radar IDs. I hope that these shortcomings are addressed, I really |
||
| 874 | # liked having the rules handled directly in Xcode during the period that |
||
| 875 | # I was prototyping this. |
||
| 876 | # |
||
| 877 | # 6588600 Xcode compiles custom script rule outputs too soon, compilation |
||
| 878 | # fails. This occurs when rule outputs from distinct inputs are |
||
| 879 | # interdependent. The only workaround is to put rules and their |
||
| 880 | # inputs in a separate target from the one that compiles the rule |
||
| 881 | # outputs. This requires input file cooperation and it means that |
||
| 882 | # process_outputs_as_sources is unusable. |
||
| 883 | # 6584932 Need to declare that custom rule outputs should be excluded from |
||
| 884 | # compilation. A possible workaround is to lie to Xcode about a |
||
| 885 | # rule's output, giving it a dummy file it doesn't know how to |
||
| 886 | # compile. The rule action script would need to touch the dummy. |
||
| 887 | # 6584839 I need a way to declare additional inputs to a custom rule. |
||
| 888 | # A possible workaround is a shell script phase prior to |
||
| 889 | # compilation that touches a rule's primary input files if any |
||
| 890 | # would-be additional inputs are newer than the output. Modifying |
||
| 891 | # the source tree - even just modification times - feels dirty. |
||
| 892 | # 6564240 Xcode "custom script" build rules always dump all environment |
||
| 893 | # variables. This is a low-prioroty problem and is not a |
||
| 894 | # show-stopper. |
||
| 895 | rules_by_ext = {} |
||
| 896 | for rule in spec_rules: |
||
| 897 | rules_by_ext[rule['extension']] = rule |
||
| 898 | |||
| 899 | # First, some definitions: |
||
| 900 | # |
||
| 901 | # A "rule source" is a file that was listed in a target's "sources" |
||
| 902 | # list and will have a rule applied to it on the basis of matching the |
||
| 903 | # rule's "extensions" attribute. Rule sources are direct inputs to |
||
| 904 | # rules. |
||
| 905 | # |
||
| 906 | # Rule definitions may specify additional inputs in their "inputs" |
||
| 907 | # attribute. These additional inputs are used for dependency tracking |
||
| 908 | # purposes. |
||
| 909 | # |
||
| 910 | # A "concrete output" is a rule output with input-dependent variables |
||
| 911 | # resolved. For example, given a rule with: |
||
| 912 | # 'extension': 'ext', 'outputs': ['$(INPUT_FILE_BASE).cc'], |
||
| 913 | # if the target's "sources" list contained "one.ext" and "two.ext", |
||
| 914 | # the "concrete output" for rule input "two.ext" would be "two.cc". If |
||
| 915 | # a rule specifies multiple outputs, each input file that the rule is |
||
| 916 | # applied to will have the same number of concrete outputs. |
||
| 917 | # |
||
| 918 | # If any concrete outputs are outdated or missing relative to their |
||
| 919 | # corresponding rule_source or to any specified additional input, the |
||
| 920 | # rule action must be performed to generate the concrete outputs. |
||
| 921 | |||
| 922 | # concrete_outputs_by_rule_source will have an item at the same index |
||
| 923 | # as the rule['rule_sources'] that it corresponds to. Each item is a |
||
| 924 | # list of all of the concrete outputs for the rule_source. |
||
| 925 | concrete_outputs_by_rule_source = [] |
||
| 926 | |||
| 927 | # concrete_outputs_all is a flat list of all concrete outputs that this |
||
| 928 | # rule is able to produce, given the known set of input files |
||
| 929 | # (rule_sources) that apply to it. |
||
| 930 | concrete_outputs_all = [] |
||
| 931 | |||
| 932 | # messages & actions are keyed by the same indices as rule['rule_sources'] |
||
| 933 | # and concrete_outputs_by_rule_source. They contain the message and |
||
| 934 | # action to perform after resolving input-dependent variables. The |
||
| 935 | # message is optional, in which case None is stored for each rule source. |
||
| 936 | messages = [] |
||
| 937 | actions = [] |
||
| 938 | |||
| 939 | for rule_source in rule.get('rule_sources', []): |
||
| 940 | rule_source_dirname, rule_source_basename = \ |
||
| 941 | posixpath.split(rule_source) |
||
| 942 | (rule_source_root, rule_source_ext) = \ |
||
| 943 | posixpath.splitext(rule_source_basename) |
||
| 944 | |||
| 945 | # These are the same variable names that Xcode uses for its own native |
||
| 946 | # rule support. Because Xcode's rule engine is not being used, they |
||
| 947 | # need to be expanded as they are written to the makefile. |
||
| 948 | rule_input_dict = { |
||
| 949 | 'INPUT_FILE_BASE': rule_source_root, |
||
| 950 | 'INPUT_FILE_SUFFIX': rule_source_ext, |
||
| 951 | 'INPUT_FILE_NAME': rule_source_basename, |
||
| 952 | 'INPUT_FILE_PATH': rule_source, |
||
| 953 | 'INPUT_FILE_DIRNAME': rule_source_dirname, |
||
| 954 | } |
||
| 955 | |||
| 956 | concrete_outputs_for_this_rule_source = [] |
||
| 957 | for output in rule.get('outputs', []): |
||
| 958 | # Fortunately, Xcode and make both use $(VAR) format for their |
||
| 959 | # variables, so the expansion is the only transformation necessary. |
||
| 960 | # Any remaning $(VAR)-type variables in the string can be given |
||
| 961 | # directly to make, which will pick up the correct settings from |
||
| 962 | # what Xcode puts into the environment. |
||
| 963 | concrete_output = ExpandXcodeVariables(output, rule_input_dict) |
||
| 964 | concrete_outputs_for_this_rule_source.append(concrete_output) |
||
| 965 | |||
| 966 | # Add all concrete outputs to the project. |
||
| 967 | pbxp.AddOrGetFileInRootGroup(concrete_output) |
||
| 968 | |||
| 969 | concrete_outputs_by_rule_source.append( \ |
||
| 970 | concrete_outputs_for_this_rule_source) |
||
| 971 | concrete_outputs_all.extend(concrete_outputs_for_this_rule_source) |
||
| 972 | |||
| 973 | # TODO(mark): Should verify that at most one of these is specified. |
||
| 974 | if int(rule.get('process_outputs_as_sources', False)): |
||
| 975 | for output in concrete_outputs_for_this_rule_source: |
||
| 976 | AddSourceToTarget(output, type, pbxp, xct) |
||
| 977 | |||
| 978 | # If the file came from the mac_bundle_resources list or if the rule |
||
| 979 | # is marked to process outputs as bundle resource, do so. |
||
| 980 | was_mac_bundle_resource = rule_source in tgt_mac_bundle_resources |
||
| 981 | if was_mac_bundle_resource or \ |
||
| 982 | int(rule.get('process_outputs_as_mac_bundle_resources', False)): |
||
| 983 | for output in concrete_outputs_for_this_rule_source: |
||
| 984 | AddResourceToTarget(output, pbxp, xct) |
||
| 985 | |||
| 986 | # Do we have a message to print when this rule runs? |
||
| 987 | message = rule.get('message') |
||
| 988 | if message: |
||
| 989 | message = gyp.common.EncodePOSIXShellArgument(message) |
||
| 990 | message = ExpandXcodeVariables(message, rule_input_dict) |
||
| 991 | messages.append(message) |
||
| 992 | |||
| 993 | # Turn the list into a string that can be passed to a shell. |
||
| 994 | action_string = gyp.common.EncodePOSIXShellList(rule['action']) |
||
| 995 | |||
| 996 | action = ExpandXcodeVariables(action_string, rule_input_dict) |
||
| 997 | actions.append(action) |
||
| 998 | |||
| 999 | if len(concrete_outputs_all) > 0: |
||
| 1000 | # TODO(mark): There's a possibilty for collision here. Consider |
||
| 1001 | # target "t" rule "A_r" and target "t_A" rule "r". |
||
| 1002 | makefile_name = '%s.make' % re.sub( |
||
| 1003 | '[^a-zA-Z0-9_]', '_' , '%s_%s' % (target_name, rule['rule_name'])) |
||
| 1004 | makefile_path = os.path.join(xcode_projects[build_file].path, |
||
| 1005 | makefile_name) |
||
| 1006 | # TODO(mark): try/close? Write to a temporary file and swap it only |
||
| 1007 | # if it's got changes? |
||
| 1008 | makefile = open(makefile_path, 'wb') |
||
| 1009 | |||
| 1010 | # make will build the first target in the makefile by default. By |
||
| 1011 | # convention, it's called "all". List all (or at least one) |
||
| 1012 | # concrete output for each rule source as a prerequisite of the "all" |
||
| 1013 | # target. |
||
| 1014 | makefile.write('all: \\\n') |
||
| 1015 | for concrete_output_index in \ |
||
| 1016 | xrange(0, len(concrete_outputs_by_rule_source)): |
||
| 1017 | # Only list the first (index [0]) concrete output of each input |
||
| 1018 | # in the "all" target. Otherwise, a parallel make (-j > 1) would |
||
| 1019 | # attempt to process each input multiple times simultaneously. |
||
| 1020 | # Otherwise, "all" could just contain the entire list of |
||
| 1021 | # concrete_outputs_all. |
||
| 1022 | concrete_output = \ |
||
| 1023 | concrete_outputs_by_rule_source[concrete_output_index][0] |
||
| 1024 | if concrete_output_index == len(concrete_outputs_by_rule_source) - 1: |
||
| 1025 | eol = '' |
||
| 1026 | else: |
||
| 1027 | eol = ' \\' |
||
| 1028 | makefile.write(' %s%s\n' % (concrete_output, eol)) |
||
| 1029 | |||
| 1030 | for (rule_source, concrete_outputs, message, action) in \ |
||
| 1031 | zip(rule['rule_sources'], concrete_outputs_by_rule_source, |
||
| 1032 | messages, actions): |
||
| 1033 | makefile.write('\n') |
||
| 1034 | |||
| 1035 | # Add a rule that declares it can build each concrete output of a |
||
| 1036 | # rule source. Collect the names of the directories that are |
||
| 1037 | # required. |
||
| 1038 | concrete_output_dirs = [] |
||
| 1039 | for concrete_output_index in xrange(0, len(concrete_outputs)): |
||
| 1040 | concrete_output = concrete_outputs[concrete_output_index] |
||
| 1041 | if concrete_output_index == 0: |
||
| 1042 | bol = '' |
||
| 1043 | else: |
||
| 1044 | bol = ' ' |
||
| 1045 | makefile.write('%s%s \\\n' % (bol, concrete_output)) |
||
| 1046 | |||
| 1047 | concrete_output_dir = posixpath.dirname(concrete_output) |
||
| 1048 | if (concrete_output_dir and |
||
| 1049 | concrete_output_dir not in concrete_output_dirs): |
||
| 1050 | concrete_output_dirs.append(concrete_output_dir) |
||
| 1051 | |||
| 1052 | makefile.write(' : \\\n') |
||
| 1053 | |||
| 1054 | # The prerequisites for this rule are the rule source itself and |
||
| 1055 | # the set of additional rule inputs, if any. |
||
| 1056 | prerequisites = [rule_source] |
||
| 1057 | prerequisites.extend(rule.get('inputs', [])) |
||
| 1058 | for prerequisite_index in xrange(0, len(prerequisites)): |
||
| 1059 | prerequisite = prerequisites[prerequisite_index] |
||
| 1060 | if prerequisite_index == len(prerequisites) - 1: |
||
| 1061 | eol = '' |
||
| 1062 | else: |
||
| 1063 | eol = ' \\' |
||
| 1064 | makefile.write(' %s%s\n' % (prerequisite, eol)) |
||
| 1065 | |||
| 1066 | # Make sure that output directories exist before executing the rule |
||
| 1067 | # action. |
||
| 1068 | if len(concrete_output_dirs) > 0: |
||
| 1069 | makefile.write('\t@mkdir -p "%s"\n' % |
||
| 1070 | '" "'.join(concrete_output_dirs)) |
||
| 1071 | |||
| 1072 | # The rule message and action have already had the necessary variable |
||
| 1073 | # substitutions performed. |
||
| 1074 | if message: |
||
| 1075 | # Mark it with note: so Xcode picks it up in build output. |
||
| 1076 | makefile.write('\t@echo note: %s\n' % message) |
||
| 1077 | makefile.write('\t%s\n' % action) |
||
| 1078 | |||
| 1079 | makefile.close() |
||
| 1080 | |||
| 1081 | # It might be nice to ensure that needed output directories exist |
||
| 1082 | # here rather than in each target in the Makefile, but that wouldn't |
||
| 1083 | # work if there ever was a concrete output that had an input-dependent |
||
| 1084 | # variable anywhere other than in the leaf position. |
||
| 1085 | |||
| 1086 | # Don't declare any inputPaths or outputPaths. If they're present, |
||
| 1087 | # Xcode will provide a slight optimization by only running the script |
||
| 1088 | # phase if any output is missing or outdated relative to any input. |
||
| 1089 | # Unfortunately, it will also assume that all outputs are touched by |
||
| 1090 | # the script, and if the outputs serve as files in a compilation |
||
| 1091 | # phase, they will be unconditionally rebuilt. Since make might not |
||
| 1092 | # rebuild everything that could be declared here as an output, this |
||
| 1093 | # extra compilation activity is unnecessary. With inputPaths and |
||
| 1094 | # outputPaths not supplied, make will always be called, but it knows |
||
| 1095 | # enough to not do anything when everything is up-to-date. |
||
| 1096 | |||
| 1097 | # To help speed things up, pass -j COUNT to make so it does some work |
||
| 1098 | # in parallel. Don't use ncpus because Xcode will build ncpus targets |
||
| 1099 | # in parallel and if each target happens to have a rules step, there |
||
| 1100 | # would be ncpus^2 things going. With a machine that has 2 quad-core |
||
| 1101 | # Xeons, a build can quickly run out of processes based on |
||
| 1102 | # scheduling/other tasks, and randomly failing builds are no good. |
||
| 1103 | script = \ |
||
| 1104 | """JOB_COUNT="$(/usr/sbin/sysctl -n hw.ncpu)" |
||
| 1105 | if [ "${JOB_COUNT}" -gt 4 ]; then |
||
| 1106 | JOB_COUNT=4 |
||
| 1107 | fi |
||
| 1108 | exec xcrun make -f "${PROJECT_FILE_PATH}/%s" -j "${JOB_COUNT}" |
||
| 1109 | exit 1 |
||
| 1110 | """ % makefile_name |
||
| 1111 | ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({ |
||
| 1112 | 'name': 'Rule "' + rule['rule_name'] + '"', |
||
| 1113 | 'shellScript': script, |
||
| 1114 | 'showEnvVarsInLog': 0, |
||
| 1115 | }) |
||
| 1116 | |||
| 1117 | if support_xct: |
||
| 1118 | support_xct.AppendProperty('buildPhases', ssbp) |
||
| 1119 | else: |
||
| 1120 | # TODO(mark): this assumes too much knowledge of the internals of |
||
| 1121 | # xcodeproj_file; some of these smarts should move into xcodeproj_file |
||
| 1122 | # itself. |
||
| 1123 | xct._properties['buildPhases'].insert(prebuild_index, ssbp) |
||
| 1124 | prebuild_index = prebuild_index + 1 |
||
| 1125 | |||
| 1126 | # Extra rule inputs also go into the project file. Concrete outputs were |
||
| 1127 | # already added when they were computed. |
||
| 1128 | groups = ['inputs', 'inputs_excluded'] |
||
| 1129 | if skip_excluded_files: |
||
| 1130 | groups = [x for x in groups if not x.endswith('_excluded')] |
||
| 1131 | for group in groups: |
||
| 1132 | for item in rule.get(group, []): |
||
| 1133 | pbxp.AddOrGetFileInRootGroup(item) |
||
| 1134 | |||
| 1135 | # Add "sources". |
||
| 1136 | for source in spec.get('sources', []): |
||
| 1137 | (source_root, source_extension) = posixpath.splitext(source) |
||
| 1138 | if source_extension[1:] not in rules_by_ext: |
||
| 1139 | # AddSourceToTarget will add the file to a root group if it's not |
||
| 1140 | # already there. |
||
| 1141 | AddSourceToTarget(source, type, pbxp, xct) |
||
| 1142 | else: |
||
| 1143 | pbxp.AddOrGetFileInRootGroup(source) |
||
| 1144 | |||
| 1145 | # Add "mac_bundle_resources" and "mac_framework_private_headers" if |
||
| 1146 | # it's a bundle of any type. |
||
| 1147 | if is_bundle: |
||
| 1148 | for resource in tgt_mac_bundle_resources: |
||
| 1149 | (resource_root, resource_extension) = posixpath.splitext(resource) |
||
| 1150 | if resource_extension[1:] not in rules_by_ext: |
||
| 1151 | AddResourceToTarget(resource, pbxp, xct) |
||
| 1152 | else: |
||
| 1153 | pbxp.AddOrGetFileInRootGroup(resource) |
||
| 1154 | |||
| 1155 | for header in spec.get('mac_framework_private_headers', []): |
||
| 1156 | AddHeaderToTarget(header, pbxp, xct, False) |
||
| 1157 | |||
| 1158 | # Add "mac_framework_headers". These can be valid for both frameworks |
||
| 1159 | # and static libraries. |
||
| 1160 | if is_bundle or type == 'static_library': |
||
| 1161 | for header in spec.get('mac_framework_headers', []): |
||
| 1162 | AddHeaderToTarget(header, pbxp, xct, True) |
||
| 1163 | |||
| 1164 | # Add "copies". |
||
| 1165 | pbxcp_dict = {} |
||
| 1166 | for copy_group in spec.get('copies', []): |
||
| 1167 | dest = copy_group['destination'] |
||
| 1168 | if dest[0] not in ('/', '$'): |
||
| 1169 | # Relative paths are relative to $(SRCROOT). |
||
| 1170 | dest = '$(SRCROOT)/' + dest |
||
| 1171 | |||
| 1172 | code_sign = int(copy_group.get('xcode_code_sign', 0)) |
||
| 1173 | settings = (None, '{ATTRIBUTES = (CodeSignOnCopy, ); }')[code_sign]; |
||
| 1174 | |||
| 1175 | # Coalesce multiple "copies" sections in the same target with the same |
||
| 1176 | # "destination" property into the same PBXCopyFilesBuildPhase, otherwise |
||
| 1177 | # they'll wind up with ID collisions. |
||
| 1178 | pbxcp = pbxcp_dict.get(dest, None) |
||
| 1179 | if pbxcp is None: |
||
| 1180 | pbxcp = gyp.xcodeproj_file.PBXCopyFilesBuildPhase({ |
||
| 1181 | 'name': 'Copy to ' + copy_group['destination'] |
||
| 1182 | }, |
||
| 1183 | parent=xct) |
||
| 1184 | pbxcp.SetDestination(dest) |
||
| 1185 | |||
| 1186 | # TODO(mark): The usual comment about this knowing too much about |
||
| 1187 | # gyp.xcodeproj_file internals applies. |
||
| 1188 | xct._properties['buildPhases'].insert(prebuild_index, pbxcp) |
||
| 1189 | |||
| 1190 | pbxcp_dict[dest] = pbxcp |
||
| 1191 | |||
| 1192 | for file in copy_group['files']: |
||
| 1193 | pbxcp.AddFile(file, settings) |
||
| 1194 | |||
| 1195 | # Excluded files can also go into the project file. |
||
| 1196 | if not skip_excluded_files: |
||
| 1197 | for key in ['sources', 'mac_bundle_resources', 'mac_framework_headers', |
||
| 1198 | 'mac_framework_private_headers']: |
||
| 1199 | excluded_key = key + '_excluded' |
||
| 1200 | for item in spec.get(excluded_key, []): |
||
| 1201 | pbxp.AddOrGetFileInRootGroup(item) |
||
| 1202 | |||
| 1203 | # So can "inputs" and "outputs" sections of "actions" groups. |
||
| 1204 | groups = ['inputs', 'inputs_excluded', 'outputs', 'outputs_excluded'] |
||
| 1205 | if skip_excluded_files: |
||
| 1206 | groups = [x for x in groups if not x.endswith('_excluded')] |
||
| 1207 | for action in spec.get('actions', []): |
||
| 1208 | for group in groups: |
||
| 1209 | for item in action.get(group, []): |
||
| 1210 | # Exclude anything in BUILT_PRODUCTS_DIR. They're products, not |
||
| 1211 | # sources. |
||
| 1212 | if not item.startswith('$(BUILT_PRODUCTS_DIR)/'): |
||
| 1213 | pbxp.AddOrGetFileInRootGroup(item) |
||
| 1214 | |||
| 1215 | for postbuild in spec.get('postbuilds', []): |
||
| 1216 | action_string_sh = gyp.common.EncodePOSIXShellList(postbuild['action']) |
||
| 1217 | script = 'exec ' + action_string_sh + '\nexit 1\n' |
||
| 1218 | |||
| 1219 | # Make the postbuild step depend on the output of ld or ar from this |
||
| 1220 | # target. Apparently putting the script step after the link step isn't |
||
| 1221 | # sufficient to ensure proper ordering in all cases. With an input |
||
| 1222 | # declared but no outputs, the script step should run every time, as |
||
| 1223 | # desired. |
||
| 1224 | ssbp = gyp.xcodeproj_file.PBXShellScriptBuildPhase({ |
||
| 1225 | 'inputPaths': ['$(BUILT_PRODUCTS_DIR)/$(EXECUTABLE_PATH)'], |
||
| 1226 | 'name': 'Postbuild "' + postbuild['postbuild_name'] + '"', |
||
| 1227 | 'shellScript': script, |
||
| 1228 | 'showEnvVarsInLog': 0, |
||
| 1229 | }) |
||
| 1230 | xct.AppendProperty('buildPhases', ssbp) |
||
| 1231 | |||
| 1232 | # Add dependencies before libraries, because adding a dependency may imply |
||
| 1233 | # adding a library. It's preferable to keep dependencies listed first |
||
| 1234 | # during a link phase so that they can override symbols that would |
||
| 1235 | # otherwise be provided by libraries, which will usually include system |
||
| 1236 | # libraries. On some systems, ld is finicky and even requires the |
||
| 1237 | # libraries to be ordered in such a way that unresolved symbols in |
||
| 1238 | # earlier-listed libraries may only be resolved by later-listed libraries. |
||
| 1239 | # The Mac linker doesn't work that way, but other platforms do, and so |
||
| 1240 | # their linker invocations need to be constructed in this way. There's |
||
| 1241 | # no compelling reason for Xcode's linker invocations to differ. |
||
| 1242 | |||
| 1243 | if 'dependencies' in spec: |
||
| 1244 | for dependency in spec['dependencies']: |
||
| 1245 | xct.AddDependency(xcode_targets[dependency]) |
||
| 1246 | # The support project also gets the dependencies (in case they are |
||
| 1247 | # needed for the actions/rules to work). |
||
| 1248 | if support_xct: |
||
| 1249 | support_xct.AddDependency(xcode_targets[dependency]) |
||
| 1250 | |||
| 1251 | if 'libraries' in spec: |
||
| 1252 | for library in spec['libraries']: |
||
| 1253 | xct.FrameworksPhase().AddFile(library) |
||
| 1254 | # Add the library's directory to LIBRARY_SEARCH_PATHS if necessary. |
||
| 1255 | # I wish Xcode handled this automatically. |
||
| 1256 | library_dir = posixpath.dirname(library) |
||
| 1257 | if library_dir not in xcode_standard_library_dirs and ( |
||
| 1258 | not xct.HasBuildSetting(_library_search_paths_var) or |
||
| 1259 | library_dir not in xct.GetBuildSetting(_library_search_paths_var)): |
||
| 1260 | xct.AppendBuildSetting(_library_search_paths_var, library_dir) |
||
| 1261 | |||
| 1262 | for configuration_name in configuration_names: |
||
| 1263 | configuration = spec['configurations'][configuration_name] |
||
| 1264 | xcbc = xct.ConfigurationNamed(configuration_name) |
||
| 1265 | for include_dir in configuration.get('mac_framework_dirs', []): |
||
| 1266 | xcbc.AppendBuildSetting('FRAMEWORK_SEARCH_PATHS', include_dir) |
||
| 1267 | for include_dir in configuration.get('include_dirs', []): |
||
| 1268 | xcbc.AppendBuildSetting('HEADER_SEARCH_PATHS', include_dir) |
||
| 1269 | for library_dir in configuration.get('library_dirs', []): |
||
| 1270 | if library_dir not in xcode_standard_library_dirs and ( |
||
| 1271 | not xcbc.HasBuildSetting(_library_search_paths_var) or |
||
| 1272 | library_dir not in xcbc.GetBuildSetting(_library_search_paths_var)): |
||
| 1273 | xcbc.AppendBuildSetting(_library_search_paths_var, library_dir) |
||
| 1274 | |||
| 1275 | if 'defines' in configuration: |
||
| 1276 | for define in configuration['defines']: |
||
| 1277 | set_define = EscapeXcodeDefine(define) |
||
| 1278 | xcbc.AppendBuildSetting('GCC_PREPROCESSOR_DEFINITIONS', set_define) |
||
| 1279 | if 'xcode_settings' in configuration: |
||
| 1280 | for xck, xcv in configuration['xcode_settings'].iteritems(): |
||
| 1281 | xcbc.SetBuildSetting(xck, xcv) |
||
| 1282 | if 'xcode_config_file' in configuration: |
||
| 1283 | config_ref = pbxp.AddOrGetFileInRootGroup( |
||
| 1284 | configuration['xcode_config_file']) |
||
| 1285 | xcbc.SetBaseConfiguration(config_ref) |
||
| 1286 | |||
| 1287 | build_files = [] |
||
| 1288 | for build_file, build_file_dict in data.iteritems(): |
||
| 1289 | if build_file.endswith('.gyp'): |
||
| 1290 | build_files.append(build_file) |
||
| 1291 | |||
| 1292 | for build_file in build_files: |
||
| 1293 | xcode_projects[build_file].Finalize1(xcode_targets, serialize_all_tests) |
||
| 1294 | |||
| 1295 | for build_file in build_files: |
||
| 1296 | xcode_projects[build_file].Finalize2(xcode_targets, |
||
| 1297 | xcode_target_to_target_dict) |
||
| 1298 | |||
| 1299 | for build_file in build_files: |
||
| 1300 | xcode_projects[build_file].Write() |
||
| 1301 |