Skip to content

Commit

Permalink
Add "standalone_static_library" target flag
Browse files Browse the repository at this point in the history
This flag is only valid for targets of type "static_library"

Adding 'standalone_static_library': 1, ensures:
1) "thin" archive optimisation is disabled for this target (it will contain
symbols)
2) The static_library will be "installed" to PRODUCT_DIR

This change re-lands r1510-1512.

Review URL: https://codereview.chromium.org/11031005/
Patch from Eric Boren <borenet@google.com>!

git-svn-id: http://gyp.googlecode.com/svn/trunk@1516 78cadc50-ecff-11dd-a971-7dbc132099af
  • Loading branch information
thakis@chromium.org committed Oct 5, 2012
1 parent 58f9b2f commit 012857d
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 9 deletions.
29 changes: 23 additions & 6 deletions pylib/gyp/generator/make.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions pylib/gyp/generator/msvs.py
Expand Up @@ -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:
Expand Down
17 changes: 14 additions & 3 deletions pylib/gyp/generator/ninja.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pylib/gyp/input.py
Expand Up @@ -83,6 +83,7 @@ def IsPathSection(section):
'rules',
'run_as',
'sources',
'standalone_static_library',
'suppress_wildcard',
'target_name',
'toolset',
Expand All @@ -106,6 +107,7 @@ def IsPathSection(section):
'libraries',
'link_settings',
'sources',
'standalone_static_library',
'target_name',
'type',
]
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions test/configurations/invalid/gyptest-configurations.py
Expand Up @@ -20,6 +20,7 @@
'libraries',
'link_settings',
'sources',
'standalone_static_library',
'target_name',
'type',
]
Expand Down
51 changes: 51 additions & 0 deletions 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,
},
}
},
],
}
@@ -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()
16 changes: 16 additions & 0 deletions 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',
],
},
],
}
7 changes: 7 additions & 0 deletions test/standalone-static-library/mylib.c
@@ -0,0 +1,7 @@
#include <stdio.h>

void print(void)
{
printf("hello from mylib.c\n");
return;
}
26 changes: 26 additions & 0 deletions 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',
],
},
],
}
7 changes: 7 additions & 0 deletions test/standalone-static-library/prog.c
@@ -0,0 +1,7 @@
extern void print(void);

int main(int argc, char *argv[])
{
print();
return 0;
}

0 comments on commit 012857d

Please sign in to comment.