#!/bin/bash # Copyright (c) 2013 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # For app bundles built with ASan, copies the runtime lib # (libclang_rt.asan_osx_dynamic.dylib), on which their executables depend, from # the compiler installation path into the bundle and fixes the dylib's install # name in the binary to be relative to @executable_path. set -e global BINARY := ""$(BUILT_PRODUCTS_DIR)/$(EXECUTABLE_PATH)"" if [[ ! -f "$BINARY" ]] { # This is neither an .app bundle nor a standalone executable. # Most certainly the script has been called for a data bundle. exit 0 } global BINARY_DIR := $[dirname $(BINARY)] # Find the link to the ASan runtime encoded in the binary. global BUILTIN_DYLIB_PATH := $[otool -L $(BINARY) | \ sed -Ene 's/^[[:blank:]]+(.*libclang_rt\.asan_.*_dynamic\.dylib).*$/\1/p] if [[ "${BUILTIN_DYLIB_PATH}" == *asan_iossim_dynamic* ]] { global ASAN_DYLIB_NAME := 'libclang_rt.asan_iossim_dynamic.dylib' } elif [[ "${BUILTIN_DYLIB_PATH}" == *asan_osx_dynamic* ]] { global ASAN_DYLIB_NAME := 'libclang_rt.asan_osx_dynamic.dylib' } if [[ -z "${BUILTIN_DYLIB_PATH}" ]] { echo "$(BINARY) does not depend on the ASan runtime library!" > !2 exit 1 } # TODO(glider): this doesn't work if we set CC and CXX to override the default # Clang. global ASAN_DYLIB := $[find \ "$(BUILT_PRODUCTS_DIR)/../../third_party/llvm-build/Release+Asserts/lib/clang/" \ -type f -path "*$(ASAN_DYLIB_NAME)] global DYLIB_BASENAME := $[basename $(ASAN_DYLIB)] if [[ "${DYLIB_BASENAME}" != "${ASAN_DYLIB_NAME}" ]] { echo "basename($(ASAN_DYLIB)) != $(ASAN_DYLIB_NAME)" > !2 exit 1 } # Check whether the directory containing the executable binary is named # "MacOS". In this case we're building a full-fledged OSX app and will put # the runtime into appname.app/Contents/Libraries/. Otherwise this is probably # an iOS gtest app, and the ASan runtime is put next to the executable. global UPPER_DIR := $[dirname $(BINARY_DIR)] if test $(UPPER_DIR) == "MacOS" { global LIBRARIES_DIR := ""$(UPPER_DIR)/Libraries"" mkdir -p $(LIBRARIES_DIR) global NEW_LC_ID_DYLIB := ""@executable_path/../Libraries/$(ASAN_DYLIB_NAME)"" } else { global LIBRARIES_DIR := $(BINARY_DIR) global NEW_LC_ID_DYLIB := ""@executable_path/$(ASAN_DYLIB_NAME)"" } cp $(ASAN_DYLIB) $(LIBRARIES_DIR) # Make LC_ID_DYLIB of the runtime copy point to its location. install_name_tool \ -id $(NEW_LC_ID_DYLIB) \ "$(LIBRARIES_DIR)/$(ASAN_DYLIB_NAME)" # Fix the rpath to the runtime library recorded in the binary. install_name_tool \ -change $(BUILTIN_DYLIB_PATH) \ $(NEW_LC_ID_DYLIB) \ $(BINARY) (CommandList children: [ (C {(set)} {(-e)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:BINARY) op: Equal rhs: {(DQ (${ VSub_Name BUILT_PRODUCTS_DIR) (/) (${ VSub_Name EXECUTABLE_PATH))} spids: [32] ) ] spids: [32] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalNot child: (BoolUnary op_id:BoolUnary_f child:{(DQ ($ VSub_Name "$BINARY"))}) ) ) terminator: ) ] action: [(C {(exit)} {(0)})] spids: [-1 59] ) ] spids: [-1 74] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:BINARY_DIR) op: Equal rhs: { (DQ (CommandSubPart command_list: (CommandList children:[(C {(dirname)} {(DQ (${ VSub_Name BINARY))})]) left_token: spids: [79 87] ) ) } spids: [77] ) ] spids: [77] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:BUILTIN_DYLIB_PATH) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(otool)} {(-L)} {(DQ (${ VSub_Name BINARY))}) (C {(sed)} {(-Ene)} { (SQ < "s/^[[:blank:]]+(.*libclang_rt\\.asan_.*_dynamic\\.dylib).*$/\\1/p" > ) } ) ] negated: False ) ] ) left_token: spids: [95 117] ) } spids: [94] ) ] spids: [94] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ (${ VSub_Name BUILTIN_DYLIB_PATH))} right: {(Lit_Other "*") (asan_iossim_dynamic) (Lit_Other "*")} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ASAN_DYLIB_NAME) op: Equal rhs: {(libclang_rt.asan_iossim_dynamic.dylib)} spids: [142] ) ] spids: [142] ) ] spids: [-1 139] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(DQ (${ VSub_Name BUILTIN_DYLIB_PATH))} right: {(Lit_Other "*") (asan_osx_dynamic) (Lit_Other "*")} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ASAN_DYLIB_NAME) op: Equal rhs: {(libclang_rt.asan_osx_dynamic.dylib)} spids: [167] ) ] spids: [167] ) ] spids: [145 164] ) ] spids: [-1 170] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id:BoolUnary_z child:{(DQ (${ VSub_Name BUILTIN_DYLIB_PATH))}) ) terminator: ) ] action: [ (SimpleCommand words: [ {(echo)} {(DQ (${ VSub_Name BINARY) (" does not depend on the ASan runtime library!"))} ] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[200])] ) (C {(exit)} {(1)}) ] spids: [-1 188] ) ] spids: [-1 208] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ASAN_DYLIB) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(find)} { (DQ (${ VSub_Name BUILT_PRODUCTS_DIR) ("/../../third_party/llvm-build/Release+Asserts/lib/clang/") ) } {(-type)} {(f)} {(-path)} {(DQ ("*") (${ VSub_Name ASAN_DYLIB_NAME))} ) ] ) left_token: spids: [218 244] ) } spids: [217] ) ] spids: [217] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:DYLIB_BASENAME) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(basename)} {(DQ (${ VSub_Name ASAN_DYLIB))})] ) left_token: spids: [248 256] ) } spids: [247] ) ] spids: [247] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobNEqual left: {(DQ (${ VSub_Name DYLIB_BASENAME))} right: {(DQ (${ VSub_Name ASAN_DYLIB_NAME))} ) ) terminator: ) ] action: [ (SimpleCommand words: [ {(echo)} { (DQ ("basename(") (${ VSub_Name ASAN_DYLIB) (") != ") (${ VSub_Name ASAN_DYLIB_NAME)) } ] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[295])] ) (C {(exit)} {(1)}) ] spids: [-1 279] ) ] spids: [-1 303] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:UPPER_DIR) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(dirname)} {(DQ (${ VSub_Name BINARY_DIR))})] ) left_token: spids: [319 327] ) } spids: [318] ) ] spids: [318] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ (${ VSub_Name UPPER_DIR))} {(Lit_Other "=") (Lit_Other "=")} {(DQ (MacOS))} {(Lit_Other "]")} ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:LIBRARIES_DIR) op: Equal rhs: {(DQ (${ VSub_Name UPPER_DIR) (/Libraries))} spids: [352] ) ] spids: [352] ) (C {(mkdir)} {(-p)} {(DQ (${ VSub_Name LIBRARIES_DIR))}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NEW_LC_ID_DYLIB) op: Equal rhs: {(DQ ("@executable_path/../Libraries/") (${ VSub_Name ASAN_DYLIB_NAME))} spids: [372] ) ] spids: [372] ) ] spids: [-1 349] ) ] else_action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:LIBRARIES_DIR) op: Equal rhs: {(DQ (${ VSub_Name BINARY_DIR))} spids: [383] ) ] spids: [383] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NEW_LC_ID_DYLIB) op: Equal rhs: {(DQ ("@executable_path/") (${ VSub_Name ASAN_DYLIB_NAME))} spids: [391] ) ] spids: [391] ) ] spids: [380 399] ) (C {(cp)} {(DQ (${ VSub_Name ASAN_DYLIB))} {(DQ (${ VSub_Name LIBRARIES_DIR))}) (C {(install_name_tool)} {(-id)} {(DQ (${ VSub_Name NEW_LC_ID_DYLIB))} {(DQ (${ VSub_Name LIBRARIES_DIR) (/) (${ VSub_Name ASAN_DYLIB_NAME))} ) (C {(install_name_tool)} {(-change)} {(DQ (${ VSub_Name BUILTIN_DYLIB_PATH))} {(DQ (${ VSub_Name NEW_LC_ID_DYLIB))} {(DQ (${ VSub_Name BINARY))} ) ] )