Skip to content
This repository was archived by the owner on Feb 4, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ CLCACHE_NODIRECT::
key. Use this if you experience problems with direct mode or if you need
built-in macroses like \__TIME__ to work correctly.
CLCACHE_BASEDIR::
Has effect only when direct mode is on. Set this to path to root directory
of your project. This allows clcache to cache relative paths, so if you
move your project to different directory, clcache will produce cache hits as
before.
Set this to path to root directory of your project. In direct mode, this allows
clcache to cache relative paths, so if you move your project to different directory,
clcache will produce cache hits as before. When direct mode is disabled, clcache will
translate any absolute paths in the preprocessor output into relative paths before
computing the hash.
CLCACHE_OBJECT_CACHE_TIMEOUT_MS::
Overrides the default ObjectCacheLock timeout (Default is 10 * 1000 ms).
The ObjectCacheLock is used to give exclusive access to the cache, which is
Expand Down
7 changes: 6 additions & 1 deletion clcache/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ def computeKeyNodirect(compilerBinary, commandLine, environment):
compilerHash = getCompilerHash(compilerBinary)
normalizedCmdLine = CompilerArtifactsRepository._normalizedCommandLine(commandLine)

if "CLCACHE_BASEDIR" in os.environ:
baseDir = normalizeBaseDir(os.environ["CLCACHE_BASEDIR"]).replace("\\", "\\\\").encode("UTF-8")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, why are backslashes escaped here?

Copy link
Author

@oktal3700 oktal3700 Feb 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to match the backslashes that are escaped in the preprocessor output because __FILE__ expands to a C string literal.

newBaseDir = BASEDIR_REPLACEMENT.encode("UTF-8")
preprocessedSourceCode = re.sub(re.escape(baseDir), newBaseDir, preprocessedSourceCode, flags=re.IGNORECASE)

h = HashAlgorithm()
h.update(compilerHash.encode("UTF-8"))
h.update(' '.join(normalizedCmdLine).encode("UTF-8"))
Expand All @@ -495,7 +500,7 @@ def _normalizedCommandLine(cmdline):
argsToStrip += ("MP",)

return [arg for arg in cmdline
if not (arg[0] in "/-" and arg[1:].startswith(argsToStrip))]
if arg[0] in "/-" and not arg[1:].startswith(argsToStrip)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, did you mean to change the behaviour here? I suspect the and should become or here. De Morgan found that not (a and b) is equivalent to not a or not b.

That being said, I'm not sure the new version is any nicer than the old one, so maybe it's not worth touching this at all.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I only now noticed that you meant to fix a bug here. In that case, can you please add a test case which demonstrates the bug?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testBasedirNoDirect would fail without this bugfix, but I can add a test that exercises only this bugfix and none of the other changes, if you like.


class CacheFileStrategy:
def __init__(self, cacheDirectory=None):
Expand Down
8 changes: 8 additions & 0 deletions tests/integrationtests/basedir/filemacro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>

int main()
{
std::cout << __FILE__ << '\n';
}


14 changes: 13 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,12 +1069,14 @@ def tearDown(self):
os.chdir(self.savedCwd)
self.tempDir.cleanup()

def _runCompiler(self, cppFile, extraArgs=None):
def _runCompiler(self, cppFile, extraArgs=None, direct=True):
cmd = CLCACHE_CMD + ["/nologo", "/EHsc", "/c"]
if extraArgs:
cmd.extend(extraArgs)
cmd.append(cppFile)
env = dict(os.environ, CLCACHE_DIR=self.clcacheDir, CLCACHE_BASEDIR=os.getcwd())
if not direct:
env["CLCACHE_NODIRECT"] = "1"
self.assertEqual(subprocess.call(cmd, env=env), 0)

def expectHit(self, runCompiler):
Expand Down Expand Up @@ -1140,6 +1142,16 @@ def runCompiler():
self._runCompiler("main.cpp", ["/DRESOURCES_DIR={}".format(os.getcwd())])
self.expectMiss([runCompiler, runCompiler])

def testBasedirNoDirectAbsolutePath(self):
def runCompiler():
self._runCompiler(os.path.join(os.getcwd(), "main.cpp"), direct=False)
self.expectHit([runCompiler, runCompiler])

def testBasedirNoDirectFileMacro(self):
def runCompiler():
self._runCompiler(os.path.join(os.getcwd(), "filemacro.cpp"), direct=False)
self.expectHit([runCompiler, runCompiler])

def testBasedirRelativeIncludeArg(self):
basedir = os.getcwd()

Expand Down