diff --git a/pylib/gyp/generator/make.py b/pylib/gyp/generator/make.py index fa730e51..eb045d8b 100644 --- a/pylib/gyp/generator/make.py +++ b/pylib/gyp/generator/make.py @@ -125,7 +125,10 @@ def ensure_directory_exists(path): LINK_COMMANDS_LINUX = """\ quiet_cmd_alink = AR($(TOOLSET)) $@ -cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^) +cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crs $@ $(filter %.o,$^) + +quiet_cmd_alink_thin = AR($(TOOLSET)) $@ +cmd_alink_thin = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^) # Due to circular dependencies between libraries :(, we wrap the # special "figure out circular dependencies" flags around the entire @@ -176,7 +179,10 @@ def ensure_directory_exists(path): LINK_COMMANDS_ANDROID = """\ quiet_cmd_alink = AR($(TOOLSET)) $@ -cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^) +cmd_alink = rm -f $@ && $(AR.$(TOOLSET)) crs $@ $(filter %.o,$^) + +quiet_cmd_alink_thin = AR($(TOOLSET)) $@ +cmd_alink_thin = rm -f $@ && $(AR.$(TOOLSET)) crsT $@ $(filter %.o,$^) # Due to circular dependencies between libraries :(, we wrap the # special "figure out circular dependencies" flags around the entire @@ -717,9 +723,12 @@ def Write(self, qualified_target, base_path, output_filename, spec, configs, else: self.output = self.output_binary = self.ComputeOutput(spec) + self.is_standalone_static_library = bool( + spec.get('standalone_static_library', 0)) self._INSTALLABLE_TARGETS = ('executable', 'loadable_module', 'shared_library') - if self.type in self._INSTALLABLE_TARGETS: + if (self.is_standalone_static_library or + self.type in self._INSTALLABLE_TARGETS): self.alias = os.path.basename(self.output) install_path = self._InstallableTargetInstallPath() else: @@ -1532,8 +1541,13 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps, for link_dep in link_deps: assert ' ' not in link_dep, ( "Spaces in alink input filenames not supported (%s)" % link_dep) - self.WriteDoCmd([self.output_binary], link_deps, 'alink', part_of_all, - postbuilds=postbuilds) + if (self.flavor not in ('mac', 'win') and not + self.is_standalone_static_library): + self.WriteDoCmd([self.output_binary], link_deps, 'alink_thin', + part_of_all, postbuilds=postbuilds) + else: + self.WriteDoCmd([self.output_binary], link_deps, 'alink', part_of_all, + postbuilds=postbuilds) elif self.type == 'shared_library': self.WriteLn('%s: LD_INPUTS := %s' % ( QuoteSpaces(self.output_binary), @@ -1573,9 +1587,12 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps, # 1) They need to install to the build dir or "product" dir. # 2) They get shortcuts for building (e.g. "make chrome"). # 3) They are part of "make all". - if self.type in self._INSTALLABLE_TARGETS: + if (self.type in self._INSTALLABLE_TARGETS or + self.is_standalone_static_library): if self.type == 'shared_library': file_desc = 'shared library' + elif self.type == 'static_library': + file_desc = 'static library' else: file_desc = 'executable' install_path = self._InstallableTargetInstallPath() diff --git a/pylib/gyp/generator/msvs.py b/pylib/gyp/generator/msvs.py index 96ccfaeb..47cbd36e 100644 --- a/pylib/gyp/generator/msvs.py +++ b/pylib/gyp/generator/msvs.py @@ -1157,6 +1157,8 @@ def _GetOutputFilePathAndTool(spec, msbuild): output_file_props = output_file_map.get(spec['type']) if output_file_props and int(spec.get('msvs_auto_output_file', 1)): vc_tool, msbuild_tool, out_dir, suffix = output_file_props + if spec.get('standalone_static_library', 0): + out_dir = '$(OutDir)' out_dir = spec.get('product_dir', out_dir) product_extension = spec.get('product_extension') if product_extension: diff --git a/pylib/gyp/generator/ninja.py b/pylib/gyp/generator/ninja.py index c11ba9e9..83e1ca83 100644 --- a/pylib/gyp/generator/ninja.py +++ b/pylib/gyp/generator/ninja.py @@ -366,6 +366,8 @@ def WriteSpec(self, spec, config_name, generator_flags, self.toolset = spec['toolset'] config = spec['configurations'][config_name] self.target = Target(spec['type']) + self.is_standalone_static_library = bool( + spec.get('standalone_static_library', 0)) self.is_mac_bundle = gyp.xcode_emulation.IsMacBundle(self.flavor, spec) self.xcode_settings = self.msvs_settings = None @@ -952,8 +954,13 @@ def WriteTarget(self, spec, config_name, config, link_deps, compile_deps): if self.xcode_settings: variables.append(('libtool_flags', self.xcode_settings.GetLibtoolflags(config_name))) - self.ninja.build(self.target.binary, 'alink', link_deps, - order_only=compile_deps, variables=variables) + if (self.flavor not in ('mac', 'win') and not + self.is_standalone_static_library): + self.ninja.build(self.target.binary, 'alink_thin', link_deps, + order_only=compile_deps, variables=variables) + else: + self.ninja.build(self.target.binary, 'alink', link_deps, + order_only=compile_deps, variables=variables) else: self.WriteLink(spec, config_name, config, link_deps) return self.target.binary @@ -1137,7 +1144,7 @@ def ComputeOutput(self, spec, type=None): elif self.flavor == 'win' and self.toolset == 'target': type_in_output_root += ['shared_library'] - if type in type_in_output_root: + if type in type_in_output_root or self.is_standalone_static_library: return filename elif type == 'shared_library': libdir = 'lib' @@ -1494,6 +1501,10 @@ def GenerateOutputForConfig(target_list, target_dicts, data, params, master_ninja.rule( 'alink', description='AR $out', + command='rm -f $out && $ar rcs $out $in') + master_ninja.rule( + 'alink_thin', + description='AR $out', command='rm -f $out && $ar rcsT $out $in') # This allows targets that only need to depend on $lib's API to declare an diff --git a/pylib/gyp/input.py b/pylib/gyp/input.py index e5eba9e0..671420f0 100644 --- a/pylib/gyp/input.py +++ b/pylib/gyp/input.py @@ -83,6 +83,7 @@ def IsPathSection(section): 'rules', 'run_as', 'sources', + 'standalone_static_library', 'suppress_wildcard', 'target_name', 'toolset', @@ -106,6 +107,7 @@ def IsPathSection(section): 'libraries', 'link_settings', 'sources', + 'standalone_static_library', 'target_name', 'type', ] @@ -2279,6 +2281,11 @@ def ValidateTargetType(target, target_dict): raise GypError("Target %s has an invalid target type '%s'. " "Must be one of %s." % (target, target_type, '/'.join(VALID_TARGET_TYPES))) + if (target_dict.get('standalone_static_library', 0) and + not target_type == 'static_library'): + raise GypError('Target %s has type %s but standalone_static_library flag is' + ' only valid for static_library type.' % (target, + target_type)) def ValidateSourcesInTarget(target, target_dict, build_file): diff --git a/test/configurations/invalid/gyptest-configurations.py b/test/configurations/invalid/gyptest-configurations.py index d76cdedb..c8b853e7 100755 --- a/test/configurations/invalid/gyptest-configurations.py +++ b/test/configurations/invalid/gyptest-configurations.py @@ -20,6 +20,7 @@ 'libraries', 'link_settings', 'sources', + 'standalone_static_library', 'target_name', 'type', ] diff --git a/test/configurations/invalid/standalone_static_library.gyp b/test/configurations/invalid/standalone_static_library.gyp new file mode 100644 index 00000000..1d64050c --- /dev/null +++ b/test/configurations/invalid/standalone_static_library.gyp @@ -0,0 +1,51 @@ +# Copyright (c) 2012 Google Inc. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'configurations', + 'type': 'none', + 'configurations': { + 'Debug': { + 'standalone_static_library': 1, + }, + } + }, + ], +} +# Copyright (c) 2012 Google Inc. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'configurations', + 'type': 'none', + 'configurations': { + 'Debug': { + 'standalone_static_library': 1, + }, + } + }, + ], +} +# Copyright (c) 2012 Google Inc. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'configurations', + 'type': 'none', + 'configurations': { + 'Debug': { + 'standalone_static_library': 1, + }, + } + }, + ], +} diff --git a/test/standalone-static-library/gyptest-standalone-static-library.py b/test/standalone-static-library/gyptest-standalone-static-library.py new file mode 100644 index 00000000..89f5cbea --- /dev/null +++ b/test/standalone-static-library/gyptest-standalone-static-library.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# Copyright (c) 2012 Google Inc. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" +Verifies build of a static_library with the standalone_static_library flag set. +""" + +import os +import subprocess +import sys +import TestGyp + +test = TestGyp.TestGyp() + +# Verify that types other than static_library cause a failure. +test.run_gyp('invalid.gyp', status=1, stderr=None) +target_str = 'invalid.gyp:bad#target' +if test.format == 'scons': + target_str = os.path.join(os.path.realpath(os.curdir), target_str) +err = ['gyp: Target %s has type executable but standalone_static_library flag ' + 'is only valid for static_library type.' % target_str] +test.must_contain_all_lines(test.stderr(), err) + +# Build a valid standalone_static_library. +test.run_gyp('mylib.gyp') +test.build('mylib.gyp', target='prog') + +# Verify that the static library is copied to the correct location. +if test.format == 'scons': + # For scons, we expect the library to be copied to the shared lib dir. + standalone_static_library_dir = test.SHARED_LIB +else: + # Otherwise, we expect the library to be copied to $PRODUCT_DIR. + standalone_static_library_dir = test.EXECUTABLE +path_to_lib = os.path.split( + test.built_file_path('mylib', type=standalone_static_library_dir))[0] +lib_name = test.built_file_basename('mylib', type=test.STATIC_LIB) +path = os.path.join(path_to_lib, lib_name) +test.must_exist(path) + +# Verify that the program runs properly. +expect = 'hello from mylib.c\n' +test.run_built_executable('prog', stdout=expect) + +# Verify that libmylib.a contains symbols. "ar -x" fails on a 'thin' archive. +if test.format in ('make', 'ninja') and sys.platform.startswith('linux'): + retcode = subprocess.call(['ar', '-x', path]) + assert retcode == 0 + +test.pass_test() \ No newline at end of file diff --git a/test/standalone-static-library/invalid.gyp b/test/standalone-static-library/invalid.gyp new file mode 100644 index 00000000..54b32117 --- /dev/null +++ b/test/standalone-static-library/invalid.gyp @@ -0,0 +1,16 @@ +# Copyright (c) 2012 Google Inc. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'bad', + 'type': 'executable', + 'standalone_static_library': 1, + 'sources': [ + 'prog.c', + ], + }, + ], +} \ No newline at end of file diff --git a/test/standalone-static-library/mylib.c b/test/standalone-static-library/mylib.c new file mode 100644 index 00000000..108be618 --- /dev/null +++ b/test/standalone-static-library/mylib.c @@ -0,0 +1,7 @@ +#include + +void print(void) +{ + printf("hello from mylib.c\n"); + return; +} diff --git a/test/standalone-static-library/mylib.gyp b/test/standalone-static-library/mylib.gyp new file mode 100644 index 00000000..2d191de3 --- /dev/null +++ b/test/standalone-static-library/mylib.gyp @@ -0,0 +1,26 @@ +# Copyright (c) 2012 Google Inc. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'mylib', + 'type': 'static_library', + 'standalone_static_library': 1, + 'sources': [ + 'mylib.c', + ], + }, + { + 'target_name': 'prog', + 'type': 'executable', + 'sources': [ + 'prog.c', + ], + 'dependencies': [ + 'mylib', + ], + }, + ], +} diff --git a/test/standalone-static-library/prog.c b/test/standalone-static-library/prog.c new file mode 100644 index 00000000..dc12b689 --- /dev/null +++ b/test/standalone-static-library/prog.c @@ -0,0 +1,7 @@ +extern void print(void); + +int main(int argc, char *argv[]) +{ + print(); + return 0; +}