Skip to content
This repository has been archived by the owner on Feb 4, 2020. It is now read-only.

Support CLCACHE_BASEDIR in nodirect mode #349

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

oktal3700
Copy link

@oktal3700 oktal3700 commented Feb 21, 2019

Do a simple case-insensitive find-and-replace to transform absolute
paths into relative paths within the preprocessor output that is used
to compute the hash. This makes CLCACHE_NODIRECT mode usable in the
presence of the __FILE__ macro.

Fixes a bug in _normalizedCommandLine that caused the source filename
to be included in the hash computation.

@swift-project-old swift-project-old force-pushed the basedir-nodirect branch 2 times, most recently from 15b347e to 33e5a0c Compare February 21, 2019 01:01
@oktal3700
Copy link
Author

Not sure what caused the build to fail.

Copy link
Owner

@frerich frerich left a comment

Choose a reason for hiding this comment

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

I think this looks like a plausible improvement, thanks a lot! However, it also invalidates all existing cache entries for people who do not use the direct mode, i.e. I guess this shouldn't be part of a bugfix release but rather of a feature release.

output += data[index:nextIndex] + new
index = nextIndex + len(old)


Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure you need a dedicated function for this -- maybe it would be viable to just use re.sub here? I think caseInsensitiveReplace is basically

re.sub(re.escape(old), lambda _: new, data, flags=re.IGNORECASE)

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.

Rightly or wrongly, for simple things I personally like to avoid regex for performance reasons. But I'm a beginner in Python so if you prefer regex here I'm happy to oblige. And re.escape should protect it from any shenanigans.

@@ -469,6 +482,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.

@@ -495,7 +513,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.

self.assertEqual(replace(b"infix paTTErn embed"), b"infix replacement embed")
self.assertEqual(replace(b"Pattern and PATTERN and pattern"), b"replacement and replacement and replacement")
self.assertEqual(replace(b"and finally PATTERN"), b"and finally replacement")

Copy link
Owner

Choose a reason for hiding this comment

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

I suppose that in case you decide to drop the custom caseInsensitiveReplace function in favor of re.sub, you could also drop this test.

@oktal3700
Copy link
Author

it also invalidates all existing cache entries for people who do not use the direct mode

Are you sure? Only computeKeyNodirect is affected. The bugfix part will invalidate cache entries for people who do not use BASEDIR though, is that what you meant?

@codecov-io
Copy link

codecov-io commented Feb 21, 2019

Codecov Report

Merging #349 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #349      +/-   ##
==========================================
+ Coverage   88.93%   88.96%   +0.03%     
==========================================
  Files           4        4              
  Lines        1301     1305       +4     
  Branches      195      196       +1     
==========================================
+ Hits         1157     1161       +4     
  Misses        106      106              
  Partials       38       38
Flag Coverage Δ
#integrationtests_memcached 67.71% <100%> (+0.1%) ⬆️
#unittests 85.9% <100%> (+0.04%) ⬆️
Impacted Files Coverage Δ
clcache/__main__.py 90.9% <100%> (+0.03%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cae73d8...863fdec. Read the comment docs.

@oktal3700
Copy link
Author

Applied suggested changes.

Do a simple case-insensitive find-and-replace to transform absolute
paths into relative paths within the preprocessor output that is used
to compute the hash. This makes CLCACHE_NODIRECT mode usable in the
presence of the __FILE__ macro.

Fixes a bug in _normalizedCommandLine that caused the source filename
to be included in the hash computation.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants