Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
395e09a
New conditional C Scanner; Improvements for virtual C Pre-Processor
ivankravets Jan 26, 2018
5fcdb30
Avoid infinite recursion for conditional PreProcessor
ivankravets Mar 8, 2018
080d987
Implement CPP tuples caching per a file
ivankravets Mar 8, 2018
80e62ed
Restore support for "all" option for Conditional PreProcessor
ivankravets Mar 8, 2018
e2312b8
Allow to control maximum depth of nested includes for conditional Pre…
ivankravets Mar 8, 2018
df1d27f
Merge branch 'master' into c-conditional-scanner
ivankravets Oct 30, 2018
0f1caa4
Merge branch 'master' of https://github.com/SCons/scons into c-condit…
ivankravets Nov 19, 2018
1ec74bc
Remember known CPPPATH for dependencies
ivankravets Nov 19, 2018
037c873
Handle CWD when searching for a file
ivankravets Nov 19, 2018
04bf778
Stop handling "define" and "undef" when condition fails
ivankravets Nov 20, 2018
bbab871
Merge branch 'rel_3.0.3' of https://github.com/SCons/scons into c-con…
ivankravets Jan 10, 2019
f522214
Merge branch 'master' of https://github.com/SCons/scons into c-condit…
ivankravets Apr 23, 2019
2530e12
Merge branch 'rel_3.1.1' of https://github.com/SCons/scons into c-con…
ivankravets Aug 26, 2019
fa2df78
Fix incorrect calculation of includes depth
ivankravets Aug 26, 2019
b9b4a8f
Remove tuples caching, save memory
ivankravets Aug 26, 2019
8511a95
Merge branch 'c-conditional-scanner' of https://github.com/platformio…
ivankravets Aug 26, 2019
3d720ad
Merge branch 'master' into c-conditional-scanner
ivankravets Dec 21, 2019
05aec82
Merge branch 'master' into c-conditional-scanner
ivankravets Feb 12, 2020
8bc1879
Update changes with CConditionalScanner and improved virtual C Pre-Pr…
ivankravets Feb 12, 2020
fe2680d
Isolate CConditionalScanner from CScanner
ivankravets Feb 13, 2020
e2f2264
Merge branch 'master' into c-conditional-scanner
bdbaddog Feb 19, 2020
5e70c65
Merge branch 'master' into c-conditional-scanner
bdbaddog Apr 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
NOTE: If your build depends on the above you must now add to your SHLIBVERSIONFLAGS
- Speedup bin/docs-update-generated by caching parsed docbook schema. (60x speedup)


From Jeremy Elson:
- Updated design doc to use the correct syntax for Depends()

Expand All @@ -60,6 +59,17 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
- Added a new flag called "linedraw" for the command line argument "--tree"
that instructs scons to use single line drawing characters to draw the dependency tree.

From Ivan Kravets, PlatformIO
- New conditional C Scanner (`SCons.Scanner.C.CConditionalScanner()`)
which interprets C/C Preprocessor conditional syntax (#ifdef, #if, #else,
#elif, #define, etc.)
- Improvements for virtual C Pre-Processor:
* Handle UNSIGNED LONG and LONG numeric constants in DEC (keep support for HEX)
* Skip unrecognized directives, such as `#if( defined ...)`
* Ignore `#include DYNAMIC_INCLUDE` directive that depends on a dynamic
macro which is not located in a state TABLE.
* Cleanup CPP expressions before evaluating (strip comments, carriage returns)

From Andrew Morrow:
- Fix Issue #3469 - Fixed improper reuse of temporary and compiled files by Configure when changing
the order and/or number of tests. This is done by using the hash of the generated temporary files
Expand Down
96 changes: 95 additions & 1 deletion src/engine/SCons/Scanner/C.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""SCons.Scanner.C

This module implements the dependency scanner for C/C++ code.
This module implements the dependency scanner for C/C++ code.

"""

Expand Down Expand Up @@ -124,6 +124,100 @@ def CScanner():
'^[ \t]*#[ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")')
return cs


#
# ConditionalScanner
#


class SConsCPPConditionalScanner(SCons.cpp.PreProcessor):
"""
SCons-specific subclass of the cpp.py module's processing.

We subclass this so that: 1) we can deal with files represented
by Nodes, not strings; 2) we can keep track of the files that are
missing.
"""

def __init__(self, *args, **kw):
SCons.cpp.PreProcessor.__init__(self, *args, **kw)
self.missing = []
self._known_paths = []

def initialize_result(self, fname):
self.result = SCons.Util.UniqueList([fname])

def find_include_file(self, t):
keyword, quote, fname = t
paths = tuple(self._known_paths) + self.searchpath[quote]
if quote == '"':
paths = (self.current_file.dir,) + paths
result = SCons.Node.FS.find_file(fname, paths)
if result:
result_path = result.get_abspath()
for p in self.searchpath[quote]:
if result_path.startswith(p.get_abspath()):
self._known_paths.append(p)
break
else:
self.missing.append((fname, self.current_file))
return result

def read_file(self, file):
try:
with open(str(file.rfile())) as fp:
return fp.read()
except EnvironmentError:
self.missing.append((file, self.current_file))
return ""


class SConsCPPConditionalScannerWrapper(object):
"""
The SCons wrapper around a cpp.py scanner.

This is the actual glue between the calling conventions of generic
SCons scanners, and the (subclass of) cpp.py class that knows how
to look for #include lines with reasonably real C-preprocessor-like
evaluation of #if/#ifdef/#else/#elif lines.
"""

def __init__(self, name, variable):
self.name = name
self.path = SCons.Scanner.FindPathDirs(variable)

def __call__(self, node, env, path=(), depth=-1):
cpp = SConsCPPConditionalScanner(
current=node.get_dir(),
cpppath=path,
dict=dictify_CPPDEFINES(env),
depth=depth,
)
result = cpp(node)
for included, includer in cpp.missing:
fmt = "No dependency generated for file: %s (included from: %s) -- file not found"
SCons.Warnings.warn(
SCons.Warnings.DependencyWarning, fmt % (included, includer)
)
return result

def recurse_nodes(self, nodes):
return nodes

def select(self, node):
return self


def CConditionalScanner():
"""
Return an advanced conditional Scanner instance for scanning source files

Interprets C/C++ Preprocessor conditional syntax
(#ifdef, #if, defined, #else, #elif, etc.).
"""
return SConsCPPConditionalScannerWrapper("CConditionalScanner", "CPPPATH")


# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
Expand Down
68 changes: 62 additions & 6 deletions src/engine/SCons/Scanner/CTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import collections
import os
import sys
import unittest

import TestCmd
Expand All @@ -45,8 +44,11 @@
# create some source files and headers:

test.write('f1.cpp',"""
#include \"f1.h\"
#ifdef INCLUDE_F2 /* multi-line comment */
#include <f2.h>
#else
#include \"f1.h\"
#endif

int main(void)
{
Expand All @@ -56,8 +58,15 @@

test.write('f2.cpp',"""
#include \"d1/f1.h\"
#include <d2/f1.h>

#if 5UL < 10 && !defined(DUMMY_MACRO) // some comment
#if NESTED_CONDITION
#include <d2/f1.h>
#endif
#else
#include \"f1.h\"
#endif

#import <f4.h>

int main(void)
Expand Down Expand Up @@ -261,7 +270,7 @@ def runTest(self):
deps = s(env.File('f2.cpp'), env, path)
headers = ['d1/f1.h', 'f1.h', 'd1/d2/f1.h', 'd1/d2/f4.h']
deps_match(self, deps, headers)

class CScannerTestCase5(unittest.TestCase):
def runTest(self):
"""Make sure files in repositories will get scanned"""
Expand All @@ -282,7 +291,7 @@ def my_rexists(s):
# Make sure rexists() got called on the file node being
# scanned, essential for cooperation with VariantDir functionality.
assert n.GetTag('rexists_called')

headers = ['f1.h', 'f2.h', 'f3-test.h',
'd1/f1.h', 'd1/f2.h', 'd1/f3-test.h']
deps_match(self, deps, headers)
Expand Down Expand Up @@ -340,7 +349,7 @@ def __call__(self, x):

# Did we catch the warning associated with not finding fb.h?
assert to.out

deps_match(self, deps, [ 'fa.h' ])
test.unlink('fa.h')

Expand Down Expand Up @@ -440,6 +449,50 @@ def runTest(self):
assert suffix in s.get_skeys(env), "%s not in skeys" % suffix


class CConditionalScannerTestCase1(unittest.TestCase):
def runTest(self):
"""Find local files with no CPPPATH"""
env = DummyEnvironment(CPPPATH=[])
s = SCons.Scanner.C.CConditionalScanner()
path = s.path(env)
deps = s(env.File('f1.cpp'), env, path)
headers = ['f1.h']
deps_match(self, deps, headers)


class CConditionalScannerTestCase2(unittest.TestCase):
def runTest(self):
"""Find local files with no CPPPATH based on #ifdef"""
env = DummyEnvironment(CPPPATH=[], CPPDEFINES=["INCLUDE_F2"])
s = SCons.Scanner.C.CConditionalScanner()
path = s.path(env)
deps = s(env.File('f1.cpp'), env, path)
headers = ['f2.h', 'fi.h']
deps_match(self, deps, headers)


class CConditionalScannerTestCase3(unittest.TestCase):
def runTest(self):
"""Find files in explicit subdirectories, ignore missing file"""
env = DummyEnvironment(
CPPPATH=[test.workpath("d1")],
CPPDEFINES=[("NESTED_CONDITION", 1)]
)
s = SCons.Scanner.C.CConditionalScanner()
deps = s(env.File('f2.cpp'), env, s.path(env))
headers = ['d1/f1.h', 'd1/d2/f1.h']
deps_match(self, deps, headers)

# disable nested conditions
env = DummyEnvironment(
CPPPATH=[test.workpath("d1")],
CPPDEFINES=[("NESTED_CONDITION", 0)]
)
s = SCons.Scanner.C.CConditionalScanner()
deps = s(env.File('f2.cpp'), env, s.path(env))
headers = ['d1/f1.h']
deps_match(self, deps, headers)


def suite():
suite = unittest.TestSuite()
Expand All @@ -457,6 +510,9 @@ def suite():
suite.addTest(CScannerTestCase13())
suite.addTest(CScannerTestCase14())
suite.addTest(CScannerTestCase15())
suite.addTest(CConditionalScannerTestCase1())
suite.addTest(CConditionalScannerTestCase2())
suite.addTest(CConditionalScannerTestCase3())
return suite

if __name__ == "__main__":
Expand Down
Loading