1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
#! /usr/bin/env python3
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Integration tests for remote_link.
#
# Usage:
#
# Ensure that rewrapper, llvm-objdump, and llvm-dwarfdump are in your
# PATH.
# Then run:
#
# tools/clang/scripts/remote_link_integration_tests.py
#
# See also remote_link_unit_tests.py, which contains unit tests and
# instructions for generating coverage information.
import remote_ld
import remote_link
from io import StringIO
import os
import re
import shlex
import subprocess
import unittest
from unittest import mock
from remote_link_test_utils import named_directory, working_directory
# Path constants.
CHROMIUM_DIR = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', '..', '..'))
LLVM_BIN_DIR = os.path.join(CHROMIUM_DIR, 'third_party', 'llvm-build',
'Release+Asserts', 'bin')
def _create_inputs(path):
"""
Creates input files under path.
"""
with open(os.path.join(path, 'main.cpp'), 'w') as f:
f.write('extern int foo();\n'
'int main(int argc, char *argv[]) {\n return foo();\n}\n')
with open(os.path.join(path, 'foo.cpp'), 'w') as f:
f.write('int foo() {\n return 12;\n}\n')
with open(os.path.join(path, 'bar.cpp'), 'w') as f:
f.write('int bar() {\n return 9;\n}\n')
def _lto_args(generate_bitcode):
"""
Returns list of arguments to clang to generate bitcode or not.
"""
if generate_bitcode:
return ['-flto=thin']
else:
return []
class RemoteLinkUnixAllowMain(remote_ld.RemoteLinkUnix):
"""
Same as remote_ld.RemoteLinkUnix, but has "main" on the allow list.
"""
def __init__(self, *args, **kwargs):
super(RemoteLinkUnixAllowMain, self).__init__(*args, **kwargs)
self.ALLOWLIST = {'main'}
class RemoteLinkWindowsAllowMain(remote_link.RemoteLinkWindows):
"""
Same as remote_ld.RemoteLinkWindows, but has "main" on the allow list.
"""
def __init__(self, *args, **kwargs):
super(RemoteLinkWindowsAllowMain, self).__init__(*args, **kwargs)
self.ALLOWLIST = {'main.exe'}
class RemoteLinkIntegrationTest(unittest.TestCase):
def clangcl(self):
return os.path.join(LLVM_BIN_DIR, 'clang-cl' + remote_link.exe_suffix())
def lld_link(self):
return os.path.join(LLVM_BIN_DIR, 'lld-link' + remote_link.exe_suffix())
def llvmar(self):
return os.path.join(LLVM_BIN_DIR, 'llvm-ar' + remote_link.exe_suffix())
def test_distributed_lto_common_objs(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
os.makedirs('obj')
subprocess.check_call([
self.clangcl(), '-c', '-Os', '-flto=thin', 'main.cpp',
'-Foobj/main.obj'
])
subprocess.check_call([
self.clangcl(), '-c', '-Os', '-flto=thin', 'foo.cpp', '-Foobj/foo.obj'
])
subprocess.check_call([
self.clangcl(), '-c', '-Os', '-flto=thin', 'bar.cpp', '-Foobj/bar.obj'
])
subprocess.check_call([
self.llvmar(), 'crsT', 'obj/foobar.lib', 'obj/bar.obj', 'obj/foo.obj'
])
with open('main.rsp', 'w') as f:
f.write('obj/main.obj\n' 'obj/foobar.lib\n')
with open('my_reclient.sh', 'w') as f:
f.write('#! /bin/sh\n\nrewrapper "$@"\n')
os.chmod('my_reclient.sh', 0o755)
rc = remote_link.RemoteLinkWindows().main([
'remote_link.py', '--wrapper', './my_reclient.sh', '--ar-path',
self.llvmar(), '--',
self.lld_link(), '-nodefaultlib', '-entry:main', '-out:main.exe',
'@main.rsp'
])
# Should succeed.
self.assertEqual(rc, 0)
# Check codegen parameters.
with open(os.path.join(d, 'lto.main.exe', 'build.ninja')) as f:
buildrules = f.read()
codegen_match = re.search('^rule codegen\\b.*?^[^ ]', buildrules,
re.MULTILINE | re.DOTALL)
self.assertIsNotNone(codegen_match)
codegen_text = codegen_match.group(0)
self.assertIn('my_reclient.sh', codegen_text)
self.assertNotIn('-flto', codegen_text)
self.assertIn('build common_objs/obj/main.obj.stamp : codegen ',
buildrules)
self.assertIn('build common_objs/obj/foo.obj.stamp : codegen ',
buildrules)
self.assertIn(' index = common_objs/empty.thinlto.bc', buildrules)
link_match = re.search('^build main.exe : native-link\\b.*?^[^ ]',
buildrules, re.MULTILINE | re.DOTALL)
self.assertIsNotNone(link_match)
link_text = link_match.group(0)
self.assertNotIn('main.exe.split.obj', link_text)
# Check that main does not call foo.
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main.exe'])
# There are no symbols in the disassembly, but we're expecting two
# functions, one of which calls the other.
self.assertTrue(b'call' in disasm or b'jmp' in disasm)
def test_distributed_lto_allowlist(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
os.makedirs('obj')
subprocess.check_call([
self.clangcl(), '-c', '-Os', '-flto=thin', '-m32', 'main.cpp',
'-Foobj/main.obj'
])
subprocess.check_call([
self.clangcl(), '-c', '-Os', '-flto=thin', '-m32', 'foo.cpp',
'-Foobj/foo.obj'
])
subprocess.check_call([
self.clangcl(), '-c', '-Os', '-flto=thin', '-m32', 'bar.cpp',
'-Foobj/bar.obj'
])
subprocess.check_call([
self.llvmar(), 'crsT', 'obj/foobar.lib', 'obj/bar.obj', 'obj/foo.obj'
])
with open('main.rsp', 'w') as f:
f.write('obj/main.obj\n' 'obj/foobar.lib\n')
rc = RemoteLinkWindowsAllowMain().main([
'remote_link.py', '--wrapper', 'rewrapper', '--ar-path',
self.llvmar(), '--',
self.lld_link(), '-nodefaultlib', '-entry:main', '-machine:X86',
'-opt:lldlto=2', '-mllvm:-import-instr-limit=10', '-out:main.exe',
'@main.rsp'
])
# Should succeed.
self.assertEqual(rc, 0)
# Check codegen parameters.
with open(os.path.join(d, 'lto.main.exe', 'build.ninja')) as f:
buildrules = f.read()
codegen_match = re.search('^rule codegen\\b.*?^[^ ]', buildrules,
re.MULTILINE | re.DOTALL)
self.assertIsNotNone(codegen_match)
codegen_text = codegen_match.group(0)
self.assertIn('rewrapper', codegen_text)
self.assertIn('-m32', codegen_text)
self.assertIn('-mllvm -import-instr-limit=10', codegen_text)
self.assertNotIn('-flto', codegen_text)
self.assertIn('build lto.main.exe/obj/main.obj.stamp : codegen ',
buildrules)
self.assertIn('build lto.main.exe/obj/foo.obj.stamp : codegen ',
buildrules)
link_match = re.search('^build main.exe : native-link\\b.*?^[^ ]',
buildrules, re.MULTILINE | re.DOTALL)
self.assertIsNotNone(link_match)
link_text = link_match.group(0)
self.assertIn('main.exe.split.obj', link_text)
# Check that main does not call foo.
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main.exe'])
# There are no symbols in the disassembly, but we're expecting a single
# function, with no calls or jmps.
self.assertNotIn(b'jmp', disasm)
self.assertNotIn(b'call', disasm)
def test_override_allowlist(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
os.makedirs('obj')
subprocess.check_call([
self.clangcl(), '-c', '-O2', '-flto=thin', 'main.cpp',
'-Foobj/main.obj'
])
subprocess.check_call([
self.clangcl(), '-c', '-O2', '-flto=thin', 'foo.cpp', '-Foobj/foo.obj'
])
rc = remote_link.RemoteLinkWindows().main([
'remote_link.py', '--generate', '--allowlist', '--ar-path',
self.llvmar(), '--',
self.lld_link(), '-nodefaultlib', '-entry:main', '-opt:lldlto=2',
'-out:main.exe', 'obj/main.obj', 'obj/foo.obj'
])
# Should succeed.
self.assertEqual(rc, 0)
# Check that we have rules for main and foo, and that they are
# not common objects.
with open(os.path.join(d, 'lto.main.exe', 'build.ninja')) as f:
buildrules = f.read()
codegen_match = re.search(r'^rule codegen\b.*?^[^ ]', buildrules,
re.MULTILINE | re.DOTALL)
self.assertIsNotNone(codegen_match)
codegen_text = codegen_match.group(0)
self.assertNotIn('-flto', codegen_text)
self.assertIn('build lto.main.exe/obj/main.obj.stamp : codegen ',
buildrules)
self.assertIn('build lto.main.exe/obj/foo.obj.stamp : codegen ',
buildrules)
link_match = re.search(r'^build main.exe : native-link\b.*?^[^ ]',
buildrules, re.MULTILINE | re.DOTALL)
self.assertIsNotNone(link_match)
class RemoteLdIntegrationTest(unittest.TestCase):
def clangxx(self):
return os.path.join(LLVM_BIN_DIR, 'clang++' + remote_link.exe_suffix())
def llvmar(self):
return os.path.join(LLVM_BIN_DIR, 'llvm-ar' + remote_link.exe_suffix())
def test_nonlto(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
subprocess.check_call(
[self.clangxx(), '-c', '-Os', 'main.cpp', '-o', 'main.o'])
subprocess.check_call(
[self.clangxx(), '-c', '-Os', 'foo.cpp', '-o', 'foo.o'])
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '--wrapper', 'rewrapper', '--ar-path',
self.llvmar(), '--',
self.clangxx(), '-fuse-ld=lld', 'main.o', 'foo.o', '-o', 'main'
])
# Should succeed.
self.assertEqual(rc, 0)
# lto.main directory should not be present.
self.assertFalse(os.path.exists(os.path.join(d, 'lto.main')))
# Check that main calls foo.
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main'])
main_idx = disasm.index(b' <main>:\n')
after_main_idx = disasm.index(b'\n\n', main_idx)
main_disasm = disasm[main_idx:after_main_idx]
self.assertIn(b'foo', main_disasm)
def test_fallback_lto(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
subprocess.check_call([
self.clangxx(), '-c', '-Os', '-flto=thin', 'main.cpp', '-o', 'main.o'
])
subprocess.check_call(
[self.clangxx(), '-c', '-Os', '-flto=thin', 'foo.cpp', '-o', 'foo.o'])
rc = remote_ld.RemoteLinkUnix().main([
'remote_ld.py', '--wrapper', 'rewrapper', '--ar-path',
self.llvmar(), '--',
self.clangxx(), '-fuse-ld=lld', '-flto=thin', 'main.o', 'foo.o', '-o',
'main'
])
# Should succeed.
self.assertEqual(rc, 0)
# lto.main directory should not be present.
self.assertFalse(os.path.exists(os.path.join(d, 'lto.main')))
# Check that main does not call foo.
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main'])
main_idx = disasm.index(b' <main>:\n')
after_main_idx = disasm.index(b'\n\n', main_idx)
main_disasm = disasm[main_idx:after_main_idx]
self.assertNotIn(b'foo', main_disasm)
def test_distributed_lto(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
subprocess.check_call([
self.clangxx(), '-c', '-Os', '-flto=thin', 'main.cpp', '-o', 'main.o'
])
subprocess.check_call(
[self.clangxx(), '-c', '-Os', '-flto=thin', 'foo.cpp', '-o', 'foo.o'])
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '-j', '16', '--ar-path',
self.llvmar(), '--',
self.clangxx(), '-fuse-ld=lld', '-flto=thin', 'main.o', 'foo.o', '-o',
'main'
])
# Should succeed.
self.assertEqual(rc, 0)
# build.ninja file should have rewrapper invocations in it.
with open(os.path.join(d, 'lto.main', 'build.ninja')) as f:
buildrules = f.read()
self.assertIn('rewrapper ', buildrules)
self.assertIn('build lto.main/main.o.stamp : codegen ', buildrules)
self.assertIn('build lto.main/foo.o.stamp : codegen ', buildrules)
# Check that main does not call foo.
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main'])
main_idx = disasm.index(b' <main>:\n')
after_main_idx = disasm.index(b'\n\n', main_idx)
main_disasm = disasm[main_idx:after_main_idx]
self.assertNotIn(b'foo', main_disasm)
def test_distributed_lto_thin_archive_same_dir(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
subprocess.check_call([
self.clangxx(), '-c', '-Os', '-flto=thin', 'main.cpp', '-o', 'main.o'
])
subprocess.check_call(
[self.clangxx(), '-c', '-Os', '-flto=thin', 'foo.cpp', '-o', 'foo.o'])
subprocess.check_call(
[self.clangxx(), '-c', '-Os', '-flto=thin', 'bar.cpp', '-o', 'bar.o'])
subprocess.check_call(
[self.llvmar(), 'crsT', 'libfoobar.a', 'bar.o', 'foo.o'])
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '--ar-path',
self.llvmar(), '--',
self.clangxx(), '-fuse-ld=lld', '-flto=thin', 'main.o', 'libfoobar.a',
'-o', 'main'
])
# Should succeed.
self.assertEqual(rc, 0)
# build.ninja file should have rewrapper invocations in it.
with open(os.path.join(d, 'lto.main', 'build.ninja')) as f:
buildrules = f.read()
self.assertIn('rewrapper ', buildrules)
self.assertIn('build lto.main/main.o.stamp : codegen ', buildrules)
self.assertIn('build lto.main/foo.o.stamp : codegen ', buildrules)
# Check that main does not call foo.
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main'])
main_idx = disasm.index(b' <main>:\n')
after_main_idx = disasm.index(b'\n\n', main_idx)
main_disasm = disasm[main_idx:after_main_idx]
self.assertNotIn(b'foo', main_disasm)
def test_distributed_lto_thin_archive_subdir(self):
self.run_archive_test(bitcode_archive=True,
bitcode_main=True,
thin_archive=True)
def test_distributed_machine_code_thin_archive_bitcode_main_subdir(self):
self.run_archive_test(bitcode_archive=False,
bitcode_main=True,
thin_archive=True)
def test_distributed_machine_code_thin_archive_subdir(self):
self.run_archive_test(bitcode_archive=False,
bitcode_main=False,
thin_archive=True)
def test_distributed_bitcode_thick_archive_subdir(self):
self.run_archive_test(bitcode_archive=True,
bitcode_main=True,
thin_archive=False)
def test_distributed_machine_code_thick_archive_subdir(self):
self.run_archive_test(bitcode_archive=False,
bitcode_main=False,
thin_archive=False)
def test_distributed_machine_code_thick_archive_bitcode_main_subdir(self):
self.run_archive_test(bitcode_archive=False,
bitcode_main=True,
thin_archive=False)
def run_archive_test(self, bitcode_archive, bitcode_main, thin_archive):
"""
Runs a test to ensure correct remote linking handling of
an archive.
Arguments:
bitcode_archive: whether the archive should contain bitcode (true)
or machine code (false)
bitcode_main: whether the main object, outside the archive, should
contain bitcode (true) or machine code (false)
thin_archive: whether to create a thin archive instead of a regular archive.
"""
with named_directory() as d, working_directory(d):
_create_inputs(d)
os.makedirs('obj')
subprocess.check_call(
[self.clangxx(), '-c', '-Os', 'main.cpp', '-o', 'obj/main.o'] +
_lto_args(bitcode_main))
subprocess.check_call(
[self.clangxx(), '-c', '-Os', 'foo.cpp', '-o', 'obj/foo.o'] +
_lto_args(bitcode_archive))
subprocess.check_call(
[self.clangxx(), '-c', '-Os', 'bar.cpp', '-o', 'obj/bar.o'] +
_lto_args(bitcode_archive))
archive_creation_arg = 'crs'
if thin_archive:
archive_creation_arg = 'crsT'
subprocess.check_call([
self.llvmar(), archive_creation_arg, 'obj/libfoobar.a', 'obj/bar.o',
'obj/foo.o'
])
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '--ar-path',
self.llvmar(), '--',
self.clangxx(), '-fuse-ld=lld', '-flto=thin', 'obj/main.o',
'obj/libfoobar.a', '-o', 'main'
])
# Should succeed.
self.assertEqual(rc, 0)
if bitcode_main or bitcode_archive:
# build.ninja file should have rewrapper invocations in it.
with open(os.path.join(d, 'lto.main', 'build.ninja')) as f:
buildrules = f.read()
self.assertIn('rewrapper ', buildrules)
if bitcode_main:
self.assertIn('build lto.main/obj/main.o.stamp : codegen ',
buildrules)
if bitcode_archive:
if thin_archive:
self.assertIn('build lto.main/obj/foo.o.stamp : codegen ',
buildrules)
else:
self.assertIn(
'build lto.main/expanded_archives/obj/libfoobar.a/' +
'foo.o.stamp : codegen ', buildrules)
# Check that main does not call foo.
if bitcode_archive:
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main'])
main_idx = disasm.index(b' <main>:\n')
after_main_idx = disasm.index(b'\n\n', main_idx)
main_disasm = disasm[main_idx:after_main_idx]
self.assertNotIn(b'foo', main_disasm)
def test_debug_params(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
os.makedirs('obj')
subprocess.check_call([
self.clangxx(), '-c', '-g', '-gsplit-dwarf', '-flto=thin', 'main.cpp',
'-o', 'obj/main.o'
])
subprocess.check_call([
self.clangxx(), '-c', '-g', '-gsplit-dwarf', '-flto=thin', 'foo.cpp',
'-o', 'obj/foo.o'
])
with open('main.rsp', 'w') as f:
f.write('obj/main.o\n' 'obj/foo.o\n')
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '--ar-path',
self.llvmar(), '--',
self.clangxx(), '-fuse-ld=lld', '-flto=thin', '-g', '-gsplit-dwarf',
'-Wl,--lto-O2', '-o', 'main', '@main.rsp'
])
# Should succeed.
self.assertEqual(rc, 0)
# Check debug info present, refers to .dwo file, and does not
# contain full debug info for foo.cpp.
dbginfo = subprocess.check_output(
['llvm-dwarfdump', '-debug-info',
'main']).decode('utf-8', 'backslashreplace')
self.assertRegexpMatches(dbginfo, '\\bDW_AT_GNU_dwo_name\\b.*\\.dwo"')
self.assertNotRegexpMatches(dbginfo, '\\bDW_AT_name\\b.*foo\\.cpp"')
def test_distributed_lto_params(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
os.makedirs('obj')
subprocess.check_call([
self.clangxx(), '-c', '-Os', '-flto=thin', '-m32', '-fsplit-lto-unit',
'-fwhole-program-vtables', 'main.cpp', '-o', 'obj/main.o'
])
subprocess.check_call([
self.clangxx(), '-c', '-Os', '-flto=thin', '-m32', '-fsplit-lto-unit',
'-fwhole-program-vtables', 'foo.cpp', '-o', 'obj/foo.o'
])
subprocess.check_call([
self.clangxx(), '-c', '-Os', '-flto=thin', '-m32', '-fsplit-lto-unit',
'-fwhole-program-vtables', 'bar.cpp', '-o', 'obj/bar.o'
])
subprocess.check_call(
[self.llvmar(), 'crsT', 'obj/libfoobar.a', 'obj/bar.o', 'obj/foo.o'])
with open('main.rsp', 'w') as f:
f.write('-fsplit-lto-unit\n'
'-fwhole-program-vtables\n'
'obj/main.o\n'
'obj/libfoobar.a\n')
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '--ar-path',
self.llvmar(), '--',
self.clangxx(), '-fuse-ld=lld', '-flto=thin', '-m32', '-Wl,-mllvm',
'-Wl,-generate-type-units', '-Wl,--lto-O2', '-o', 'main',
'-Wl,--start-group', '@main.rsp', '-Wl,--end-group'
])
# Should succeed.
self.assertEqual(rc, 0)
# Check codegen parameters.
with open(os.path.join(d, 'lto.main', 'build.ninja')) as f:
buildrules = f.read()
codegen_match = re.search('^rule codegen\\b.*?^[^ ]', buildrules,
re.MULTILINE | re.DOTALL)
self.assertIsNotNone(codegen_match)
codegen_text = codegen_match.group(0)
self.assertIn('rewrapper', codegen_text)
self.assertIn('-m32', codegen_text)
self.assertIn('-mllvm -generate-type-units', codegen_text)
self.assertNotIn('-flto', codegen_text)
self.assertIn('build lto.main/obj/main.o.stamp : codegen ', buildrules)
self.assertIn('build lto.main/obj/foo.o.stamp : codegen ', buildrules)
link_match = re.search('^build main : native-link\\b.*?^[^ ]',
buildrules, re.MULTILINE | re.DOTALL)
self.assertIsNotNone(link_match)
link_text = link_match.group(0)
self.assertIn('main.split.o', link_text)
# Check that main does not call foo.
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main'])
main_idx = disasm.index(b' <main>:\n')
after_main_idx = disasm.index(b'\n\n', main_idx)
main_disasm = disasm[main_idx:after_main_idx]
self.assertNotIn(b'foo', main_disasm)
def test_no_rewrapper(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
subprocess.check_call([
self.clangxx(), '-c', '-Os', '-flto=thin', 'main.cpp', '-o', 'main.o'
])
subprocess.check_call(
[self.clangxx(), '-c', '-Os', '-flto=thin', 'foo.cpp', '-o', 'foo.o'])
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '--ar-path',
self.llvmar(), '--no-wrapper', '-j', '16', '--',
self.clangxx(), '-fuse-ld=lld', '-flto=thin', 'main.o', 'foo.o', '-o',
'main'
])
# Should succeed.
self.assertEqual(rc, 0)
# build.ninja file should not have rewrapper invocations in it.
with open(os.path.join(d, 'lto.main', 'build.ninja')) as f:
buildrules = f.read()
self.assertNotIn('rewrapper ', buildrules)
self.assertIn('build lto.main/main.o.stamp : codegen ', buildrules)
self.assertIn('build lto.main/foo.o.stamp : codegen ', buildrules)
# Check that main does not call foo.
disasm = subprocess.check_output(['llvm-objdump', '-d', 'main'])
main_idx = disasm.index(b' <main>:\n')
after_main_idx = disasm.index(b'\n\n', main_idx)
main_disasm = disasm[main_idx:after_main_idx]
self.assertNotIn(b'foo', main_disasm)
def test_generate_no_codegen(self):
with named_directory() as d, working_directory(d):
with open('main.o', 'wb') as f:
f.write(b'\7fELF')
with mock.patch('sys.stderr', new_callable=StringIO) as stderr:
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '--ar-path',
self.llvmar(), '--generate', '--',
self.clangxx(), 'main.o', '-o', 'main'
])
self.assertEqual(rc, 5)
self.assertIn('no ninja file generated.\n', stderr.getvalue())
def test_generate(self):
with named_directory() as d, working_directory(d):
with open('main.o', 'wb') as f:
f.write(b'BC\xc0\xde')
with mock.patch('sys.stderr', new_callable=StringIO) as stderr:
rc = RemoteLinkUnixAllowMain().main([
'remote_ld.py', '--ar-path',
self.llvmar(), '--generate', '--',
self.clangxx(), 'main.o', '-o', 'main'
])
self.assertEqual(rc, 0)
m = re.search('ninja file (.*)', stderr.getvalue())
self.assertIsNotNone(m)
path = shlex.split(m.group(1))[0]
self.assertTrue(os.path.exists(path))
content = open(path).read()
self.assertRegex(
content,
re.compile('^build [^:]+/main\\.o\\.stamp : codegen ',
re.MULTILINE))
def test_override_allowlist(self):
with named_directory() as d, working_directory(d):
_create_inputs(d)
subprocess.check_call([
self.clangxx(), '-c', '-Os', '-flto=thin', 'main.cpp', '-o', 'main.o'
])
subprocess.check_call(
[self.clangxx(), '-c', '-Os', '-flto=thin', 'foo.cpp', '-o', 'foo.o'])
rc = remote_ld.RemoteLinkUnix().main([
'remote_ld.py', '--ar-path',
self.llvmar(), '--generate', '--allowlist', '--',
self.clangxx(), '-fuse-ld=lld', '-flto=thin', 'main.o', 'foo.o', '-o',
'main'
])
# Should succeed.
self.assertEqual(rc, 0)
# build.ninja file should have rules for main and foo.
ninjafile = os.path.join(d, 'lto.main', 'build.ninja')
self.assertTrue(os.path.exists(ninjafile))
with open(ninjafile) as f:
buildrules = f.read()
self.assertIn('build lto.main/main.o.stamp : codegen ', buildrules)
self.assertIn('build lto.main/foo.o.stamp : codegen ', buildrules)
if __name__ == '__main__':
unittest.main()
|