1#!/usr/bin/env python
2
3# Copyright (c) 2012 Google Inc. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""
8Verifies build of a static_library with the standalone_static_library flag set.
9"""
10
11import os
12import subprocess
13import sys
14import TestGyp
15
16# standalone_static_library currently means two things: a specific output
17# location for the built target and non-thin archive files. The Android gyp
18# generator leaves both decisions to the Android build system, so this test
19# doesn't work for that format.
20test = TestGyp.TestGyp(formats=['!android'])
21
22# Verify that types other than static_library cause a failure.
23test.run_gyp('invalid.gyp', status=1, stderr=None)
24target_str = 'invalid.gyp:bad#target'
25err = ['gyp: Target %s has type executable but standalone_static_library flag '
26       'is only valid for static_library type.' % target_str]
27test.must_contain_all_lines(test.stderr(), err)
28
29# Build a valid standalone_static_library.
30test.run_gyp('mylib.gyp')
31test.build('mylib.gyp', target='prog')
32
33# Verify that the static library is copied to the correct location.
34# We expect the library to be copied to $PRODUCT_DIR.
35standalone_static_library_dir = test.EXECUTABLE
36path_to_lib = os.path.split(
37    test.built_file_path('mylib', type=standalone_static_library_dir))[0]
38lib_name = test.built_file_basename('mylib', type=test.STATIC_LIB)
39path = os.path.join(path_to_lib, lib_name)
40test.must_exist(path)
41
42# Verify that the program runs properly.
43expect = 'hello from mylib.c\n'
44test.run_built_executable('prog', stdout=expect)
45
46# Verify that libmylib.a contains symbols.  "ar -x" fails on a 'thin' archive.
47supports_thick = ('make', 'ninja', 'cmake')
48if test.format in supports_thick and sys.platform.startswith('linux'):
49  retcode = subprocess.call(['ar', '-x', path])
50  assert retcode == 0
51
52test.pass_test()
53