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 """ 8 Verifies build of an executable with C++ define specified by a gyp define, and 9 the use of the environment during regeneration when the gyp file changes. 10 """ 11 12 import os 13 import sys 14 import TestGyp 15 16 env_stack = [] 17 18 19 def PushEnv(): 20 env_copy = os.environ.copy() 21 env_stack.append(env_copy) 22 23 def PopEnv(): 24 os.environ.clear() 25 os.environ.update(env_stack.pop()) 26 27 formats = ['make', 'ninja'] 28 29 test = TestGyp.TestGyp(formats=formats) 30 31 try: 32 PushEnv() 33 os.environ['CFLAGS'] = '' 34 os.environ['GYP_CROSSCOMPILE'] = '1' 35 test.run_gyp('cflags.gyp') 36 test.build('cflags.gyp') 37 finally: 38 # We clear the environ after calling gyp. When the auto-regeneration happens, 39 # the same define should be reused anyway. Reset to empty string first in 40 # case the platform doesn't support unsetenv. 41 PopEnv() 42 43 44 expect = """FOO not defined\n""" 45 test.run_built_executable('cflags', stdout=expect) 46 test.run_built_executable('cflags_host', stdout=expect) 47 48 test.sleep() 49 50 try: 51 PushEnv() 52 os.environ['CFLAGS'] = '-DFOO=1' 53 os.environ['GYP_CROSSCOMPILE'] = '1' 54 test.run_gyp('cflags.gyp') 55 test.build('cflags.gyp') 56 finally: 57 # We clear the environ after calling gyp. When the auto-regeneration happens, 58 # the same define should be reused anyway. Reset to empty string first in 59 # case the platform doesn't support unsetenv. 60 PopEnv() 61 62 63 expect = """FOO defined\n""" 64 test.run_built_executable('cflags', stdout=expect) 65 66 # Environment variables shouldn't influence the flags for the host. 67 expect = """FOO not defined\n""" 68 test.run_built_executable('cflags_host', stdout=expect) 69 70 test.sleep() 71 72 try: 73 PushEnv() 74 os.environ['CFLAGS'] = '' 75 test.run_gyp('cflags.gyp') 76 test.build('cflags.gyp') 77 finally: 78 # We clear the environ after calling gyp. When the auto-regeneration happens, 79 # the same define should be reused anyway. Reset to empty string first in 80 # case the platform doesn't support unsetenv. 81 PopEnv() 82 83 84 expect = """FOO not defined\n""" 85 test.run_built_executable('cflags', stdout=expect) 86 87 test.sleep() 88 89 try: 90 PushEnv() 91 os.environ['CFLAGS'] = '-DFOO=1' 92 test.run_gyp('cflags.gyp') 93 test.build('cflags.gyp') 94 finally: 95 # We clear the environ after calling gyp. When the auto-regeneration happens, 96 # the same define should be reused anyway. Reset to empty string first in 97 # case the platform doesn't support unsetenv. 98 PopEnv() 99 100 101 expect = """FOO defined\n""" 102 test.run_built_executable('cflags', stdout=expect) 103 104 test.pass_test() 105