17 import sys, os, subprocess, locale, re
20 if len(sys.argv) != 3:
21 ourname = os.path.basename(sys.argv[0])
22 print(
'Syntax: ' + ourname +
' <dir> <build-dir>' )
23 print(
' build unit-testing framework for the tree in $dir' )
27 if not os.path.isdir( dir ):
28 print(
'FATAL Directory not found:', dir )
30 if not os.path.isdir( builddir ):
31 print(
'FATAL Directory not found:', builddir )
45 for (path,subdirs,leafs)
in os.walk( root ):
48 yield os.path.relpath( path +
'/' + leaf, root ).replace(
'\\',
'/' )
50 pattern = re.compile( mask )
52 if pattern.search( leaf ):
63 def grep_( pattern, lines, context ):
68 match = pattern.search( line )
70 context[
'index'] = index
71 context[
'line'] = line
72 context[
'match'] = match
83 pattern = re.compile( expr )
86 context[
'filename'] = filename
87 with open( filename, errors =
'ignore' )
as file:
91 librealsense = os.path.dirname(os.path.dirname(os.path.abspath(__file__))).replace(
'\\',
'/')
92 src = librealsense +
'/src' 95 makefile = builddir +
'/' + testdir +
'/CMakeLists.txt' 96 debug(
' creating:', makefile )
97 handle = open( makefile,
'w' );
98 filelist =
'\n '.
join( filelist )
100 # This file is automatically generated!! 101 # Do not modify or your changes will be lost! 103 cmake_minimum_required( VERSION 3.1.0 ) 104 project( ''' + testname +
''' ) 106 set( SRC_FILES ''' + filelist +
''' 108 add_executable( ''' + testname +
''' ${SRC_FILES} ) 109 source_group( "Common Files" FILES ${ELPP_FILES} ${CATCH_FILES} ) 110 set_property(TARGET ''' + testname +
''' PROPERTY CXX_STANDARD 11) 111 target_link_libraries( ''' + testname +
''' ${DEPENDENCIES}) 113 set_target_properties( ''' + testname +
''' PROPERTIES FOLDER "Unit-Tests/''' + os.path.dirname( testdir ) +
'''" ) 115 target_include_directories(''' + testname +
''' PRIVATE ''' + src +
''') 128 filedir = os.path.dirname(filepath)
129 for context
in grep(
'^\s*#\s*include\s+"(.*)"\s*$', filepath ):
131 index = context[
'index']
133 if not os.path.isabs( include ):
134 include = os.path.normpath( filedir +
'/' + include )
135 include = include.replace(
'\\',
'/' )
136 if os.path.exists( include ):
137 filelist.add( include )
145 for f
in find( dir,
'(^|/)test-.*\.cpp$' ):
146 testdir = os.path.splitext( f )[0]
147 testparent = os.path.dirname(testdir)
149 os.makedirs( builddir +
'/' + testdir, exist_ok =
True );
151 testname =
'test-' + testparent.replace(
'/',
'-' ) +
'-' + os.path.basename(testdir)[5:]
154 filelist = [ dir +
'/' + f,
'${ELPP_FILES}',
'${CATCH_FILES}' ]
162 for context
in grep(
'^//#cmake:\s*', dir +
'/' + f ):
164 index = context[
'index']
165 cmd, *rest = context[
'line'][m.end():].
split()
166 if cmd ==
'add-file':
167 for additional_file
in rest:
168 files = additional_file
169 if not os.path.isabs( additional_file ):
170 files = dir +
'/' + testparent +
'/' + additional_file
171 files =
glob( files )
173 error( f +
'+' +
str(index) +
': no files match "' + additional_file +
'"' )
174 for abs_file
in files:
175 abs_file = os.path.normpath( abs_file )
176 abs_file = abs_file.replace(
'\\',
'/' )
177 if not os.path.exists( abs_file ):
178 error( f +
'+' +
str(index) +
': file not found "' + additional_file +
'"' )
179 debug(
' add file:', abs_file )
180 filelist.append( abs_file )
181 if( os.path.splitext( abs_file )[0] ==
'cpp' ):
184 elif cmd ==
'static!':
186 error( f +
'+' +
str(index) +
': unexpected arguments past \'' + cmd +
'\'' )
188 error( f +
'+' +
str(index) +
': \'' + cmd +
'\' mutually exclusive with \'shared!\'' )
191 elif cmd ==
'shared!':
193 error( f +
'+' +
str(index) +
': unexpected arguments past \'' + cmd +
'\'' )
195 error( f +
'+' +
str(index) +
': \'' + cmd +
'\' mutually exclusive with \'static!\'' )
199 error( f +
'+' +
str(index) +
': unknown cmd \'' + cmd +
'\' (should be \'add-file\', \'static!\', or \'shared!\')' )
200 for include
in includes:
201 filelist.append( include )
204 statics.append( testdir )
206 shareds.append( testdir )
208 found.append( testdir )
209 return found, shareds, statics
218 normal_tests.extend( n )
219 shared_tests.extend( sh )
220 static_tests.extend( st )
222 normal_tests.extend( n )
223 shared_tests.extend( sh )
224 static_tests.extend( st )
226 cmakefile = builddir +
'/CMakeLists.txt' 227 name = os.path.basename( os.path.realpath( dir ))
228 debug(
'Creating "' + name +
'" project in', cmakefile )
230 handle = open( cmakefile,
'w' );
233 # We make use of ELPP (EasyLogging++): 234 include_directories( ''' + dir +
'''/../third-party/easyloggingpp/src ) 236 ''' + dir +
'''/../third-party/easyloggingpp/src/easylogging++.cc 237 ''' + dir +
'''/../third-party/easyloggingpp/src/easylogging++.h 240 ''' + dir +
'''/catch/catch.hpp 246 for sdir
in normal_tests:
247 handle.write(
'add_subdirectory( ' + sdir +
' )\n' )
248 debug(
'... including:', sdir )
250 if len(shared_tests):
251 handle.write(
'if(NOT ${BUILD_SHARED_LIBS})\n' )
252 handle.write(
' message( INFO "' +
str(len(shared_tests)) +
' shared lib unit-tests will be skipped. Check BUILD_SHARED_LIBS to run them..." )\n' )
253 handle.write(
'else()\n' )
254 for test
in shared_tests:
255 handle.write(
' add_subdirectory( ' + test +
' )\n' )
256 debug(
'... including:', sdir )
258 handle.write(
'endif()\n' )
259 if len(static_tests):
260 handle.write(
'if(${BUILD_SHARED_LIBS})\n' )
261 handle.write(
' message( INFO "' +
str(len(static_tests)) +
' static lib unit-tests will be skipped. Uncheck BUILD_SHARED_LIBS to run them..." )\n' )
262 handle.write(
'else()\n' )
263 for test
in static_tests:
264 handle.write(
' add_subdirectory( ' + test +
' )\n' )
265 debug(
'... including:', sdir )
267 handle.write(
'endif()\n' )
270 print(
'Generated ' +
str(n_tests) +
' unit-tests' )
std::string join(const std::string &base, const std::string &path)
std::vector< uint32_t > split(const std::string &s, char delim)
static void glob(const std::string &directory, const std::string &spec, std::function< void(std::string const &) > fn, bool recursive=true, bool includeDirectories=false)
def find_includes(filepath)
static std::string print(const transformation &tf)
static const textual_icon exit
def grep_(pattern, lines, context)
def process_py(dir, builddir)
def generate_cmake(builddir, testdir, testname, filelist)
def process_cpp(dir, builddir)
def remove_newlines(lines)