d127ccfeeeb5316463f782907e87e4a477982a05
[platform/upstream/iotivity.git] / extlibs / gtest / SConscript
1 # -*- mode: python; python-indent-offset: 4; indent-tabs-mode: nil -*-
2 ##
3 # 'googletest' script to check if Google Unit Test library is installed.
4 # If not, get it and install it
5 #
6 ##
7
8 import os
9
10 Import('env')
11
12 gtest_env = env.Clone()
13 target_os = gtest_env.get('TARGET_OS')
14 src_dir = gtest_env.get('SRC_DIR')
15
16 targets_need_gtest = ['darwin','linux', 'msys_nt', 'windows']
17 gtest_dir      = os.path.join(src_dir, 'extlibs', 'gtest', 'gtest-1.7.0')
18 gtest_lib_dir = os.path.join(gtest_dir,'lib')
19 gtest_dotlib_dir =  os.path.join(gtest_lib_dir, '.libs')
20 gtest_zip_file = os.path.join(src_dir, 'extlibs', 'gtest', 'gtest-1.7.0.zip')
21 gtest_url = 'http://pkgs.fedoraproject.org/repo/pkgs/gtest/gtest-1.7.0.zip/2d6ec8ccdf5c46b05ba54a9fd1d130d7/gtest-1.7.0.zip'
22
23 if target_os in targets_need_gtest:
24         print '*** Checking for installation of google unit test 1.7.0 ***'
25         if not os.path.exists(os.path.join(gtest_dir, 'configure')):
26                 # If the gtest zip file is not already present, download it
27                 if not os.path.exists(gtest_zip_file):
28                         gtest_zip = gtest_env.Download(gtest_zip_file, gtest_url)
29                 else:
30                         gtest_zip = gtest_zip_file
31                 print 'Unzipping to : ' + gtest_dir
32                 gtest_env.UnpackAll(gtest_dir, gtest_zip)
33
34 if target_os == 'darwin':
35         if os.path.exists(gtest_dir):
36                 # Build gtest and store it at a temporary directory
37
38                 if not os.path.exists(gtest_lib_dir):
39 #                       print 'Create a directory'
40 #                       os.mkdir(gtest_lib_dir)
41
42
43                         print 'Invoke cmake command to generate appropriate make files'
44                         gtest_env.Configure(gtest_dir, './configure')
45
46                         # Run make on gtest
47                         print 'Making google unit test'
48                         gtest_env.Configure(gtest_dir, 'make')
49
50 #                       print 'Create a directory'
51 #                       os.mkdir(gtest_dotlib_dir)
52
53                         print 'Change to a directory'
54                         os.chdir(gtest_dotlib_dir)
55
56 ##                      print 'Change to a directory'
57 #                       os.chdir(gtest_lib_dir)
58
59                         print 'Create hard links pointing to gtest libraries'
60                         os.link('libgtest.a', gtest_lib_dir + 'libgtest.a')
61                         os.link('libgtest_main.a', gtest_lib_dir +  'libgtest_main.a')
62                         print 'Create hard links pointing to gtest libraries - DONE'
63
64
65 elif target_os in ['linux']:
66         if os.path.exists(gtest_dir):
67                 if not os.path.exists(gtest_lib_dir):
68                         # Run configure on gtest
69                         print 'Configuring google unit test'
70                         if env.get('CROSS_COMPILE'):
71                                 env.Configure(gtest_dir, './configure --disable-shared --host=' + env['CROSS_COMPILE'])
72                         else:
73                                 env.Configure(gtest_dir, './configure --disable-shared')
74                         # Run make on gtest
75                         print 'Making google unit test'
76                         gtest_env.Configure(gtest_dir, 'make')
77
78 elif target_os == 'msys_nt':
79         if os.path.exists(gtest_dir):
80                 if not os.path.exists(gtest_lib_dir):
81                         # Create lib dir
82                         os.mkdir(gtest_lib_dir)
83                         os.mkdir(gtest_dotlib_dir)
84
85                         # Run configure on gtest
86                         print 'Configuring google unit test for compilation'
87                         gtest_env.Configure(gtest_dir, 'cmake -G "Unix Makefiles" .')
88
89                         # Run make on gtest
90                         print 'Making google unit test'
91                         gtest_env.Configure(gtest_dir, 'make')
92
93                         print 'Moving libraries to lib folder'
94                         gtest_env.Configure(gtest_dir, 'cp libgtest.a lib')
95                         gtest_env.Configure(gtest_dir, 'mv libgtest.a lib/.libs')
96                         gtest_env.Configure(gtest_dir, 'cp libgtest_main.a lib')
97                         gtest_env.Configure(gtest_dir, 'mv libgtest_main.a lib/.libs')
98
99 elif target_os == 'windows':
100         # Avoid building the same StaticLibrary in more than one environment, by using the
101         # IOTIVITY_GTEST_HAS_BEEN_BUILT environment variable
102         if not env.has_key('IOTIVITY_GTEST_HAS_BEEN_BUILT'):
103                 gtest_env.Append(CPPPATH = [ gtest_dir ])
104                 gtest = gtest_env.StaticLibrary(target = 'gtest', source = [ '%s/src/gtest-all.cc' % gtest_dir ])
105                 gtest_main = gtest_env.StaticLibrary(target = 'gtest_main', source = [ '%s/src/gtest_main.cc' % gtest_dir ])
106                 gtest_env.InstallTarget(gtest, 'gtest')
107                 gtest_env.InstallTarget(gtest_main, 'gtest_main')
108
109                 vars = Variables();
110                 vars.AddVariables(('IOTIVITY_GTEST_HAS_BEEN_BUILT', '', '1'))
111                 vars.Update(env)
112
113 # Export flags once for all
114 if target_os in targets_need_gtest:
115         gtest_env.AppendUnique(LIBPATH = [gtest_dotlib_dir])
116         gtest_env.PrependUnique(CPPPATH = [os.path.join(gtest_dir, 'include')])
117         gtest_env.AppendENVPath('LD_LIBRARY_PATH', gtest_dotlib_dir)
118         if 'g++' in gtest_env.get('CXX'):
119                 gtest_env.AppendUnique(CXXFLAGS = ['-std=c++0x'])
120                 gtest_env.AppendUnique(CXXFLAGS = ['-Wall'])
121 # Note: 'pthread' for android is in bionic 
122 # On other platform, if use new gcc(>4.9?) it isn't required, otherwise, it's required
123                 if target_os not in ['android']:
124                         gtest_env.AppendUnique(CXXFLAGS = ['-pthread'])
125                         gtest_env.PrependUnique(LIBS = ['pthread'])
126         gtest_env.PrependUnique(LIBS = ['gtest', 'gtest_main'])
127         if target_os in ['windows']:
128                 gtest_env.AppendUnique(LINKFLAGS = ['/subsystem:CONSOLE'])
129
130 Return('gtest_env')