1#!/bin/bash 2# Copyright 2021 Google LLC 3# 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7export LD_LIBRARY_PATH="external/clang_linux_amd64/usr/lib/x86_64-linux-gnu" 8 9set -euo pipefail 10 11if [[ "$@" == *SKIA_SKIP_LINKING* ]]; then 12 # The output executable binary file is listed as the second argument to this script, and we must 13 # make sure it exists or Bazel will fail a validation step. 14 touch $2 15 exit 0 16fi 17 18# We only want to run include-what-you-use if DSKIA_ENFORCE_IWYU is in the arguments 19# passed in (i.e. the "skia_enforce_iwyu" feature is enabled) and we are not linking 20# (as detected by the presence of -fuse-ld). 21if [[ "$@" != *DSKIA_ENFORCE_IWYU* || "$@" == *use-ld* ]]; then 22 external/clang_linux_amd64/bin/clang $@ 23 exit 0 24fi 25 26supported_files_or_dirs=( 27 "gm/" 28 "include/core/" 29 "include/effects/" 30 "include/encode/" 31 "include/gpu/ganesh/gen/" 32 "include/gpu/gen/" 33 "include/gpu/vk/gen/" 34 "include/private/" 35 "modules/bentleyottmann/" 36 "modules/skottie/" 37 "modules/sksg/" 38 "modules/skshaper/" 39 "modules/skunicode/" 40 "modules/svg/" 41 "src/base/" 42 "src/codec/" 43 "src/core/" 44 "src/effects/" 45 "src/encode/" 46 "src/gpu/ganesh/" 47 "src/gpu/graphite/compute/" 48 "src/gpu/graphite/geom/" 49 "src/gpu/graphite/render/" 50 "src/gpu/tessellate/" 51 "src/gpu/gen/" 52 "src/gpu/vk/" 53 "src/image/" 54 "src/pathops/" 55 "src/pdf/" 56 "src/ports/SkFontMgr_fontconfig" 57 "src/ports/SkFontMgr_fontations" 58 "src/shaders/" 59 "src/sksl/" 60 "src/svg/" 61 "src/text/" 62 "src/utils/" 63 "tests/" 64 "tools/debugger/" 65 "tools/viewer/" 66 "src/gpu/A" 67 "src/gpu/B" 68 "src/gpu/C" 69 "src/gpu/D" 70 "src/gpu/G" 71 "src/gpu/J" 72 "src/gpu/K" 73 "src/gpu/M" 74 "src/gpu/P" 75 "src/gpu/R" 76 "src/gpu/S" 77 "src/gpu/MutableTextureState.cpp" 78 "tools/DecodeUtils.cpp" 79 "tools/EncodeUtils.cpp" 80 "tools/GpuToolUtils.cpp" 81 "tools/Resources.cpp" 82 "tools/SvgPathExtractor.cpp" 83 "tools/ToolUtils.cpp" 84 "tools/fonts/FontToolUtils.cpp" 85) 86 87excluded_files=( 88# Causes IWYU 8.17 to assert because it includes SkVX.h 89# "iwyu.cc:1977: Assertion failed: TODO(csilvers): for objc and clang lang extensions" 90 "tests/SkVxTest.cpp" 91 "src/base/SkHalf.cpp" 92 "src/core/SkMipmap.cpp" 93 "src/core/SkMipmapHQDownSampler.cpp" 94 "src/core/SkMaskBlurFilter.cpp" 95 "src/core/SkM44.cpp" 96 "src/core/SkPixmap.cpp" 97 "modules/skottie/src/effects/MotionBlurEffect.cpp" 98# This file sets and checks for defines in a way that confuses IWYU 99 "src/gpu/vk/vulkanmemoryallocator/VulkanMemoryAllocatorWrapper.cpp" 100) 101 102function opted_in_to_IWYU_checks() { 103 # Need [@] for entire list: https://stackoverflow.com/a/46137325 104 for path in ${supported_files_or_dirs[@]}; do 105 # If this was a generated file, it will be in a different subdirectory, starting with 106 # bazel-out, (e.g. bazel-out/k8-iwyu-dbg/bin/src/gen/SkRefCnt.cpp) so check that location also. 107 if [[ $1 == *"-c $path"* ]] || [[ $1 == *"-c bazel-out"*"$path"* ]]; then 108 for e_path in ${excluded_files[@]}; do 109 if [[ $1 == *"-c $e_path"* ]]; then 110 echo "" 111 return 0 112 fi 113 done 114 echo $path 115 return 0 116 fi 117 done 118 echo "" 119 return 0 120} 121 122# We want to concatenate all args into a string so we can do some 123# string matching in the opted_in_to_IWYU_checks function. 124# https://unix.stackexchange.com/a/197794 125opt_in=$(opted_in_to_IWYU_checks "'$*'") 126if [[ -z $opt_in ]]; then 127 external/clang_linux_amd64/bin/clang $@ 128 exit 0 129else 130 # IWYU always [1] returns a non-zero code because it doesn't produce the .o file (that's why 131 # we ran Clang first). As such, we do not want bash to fail after running IWYU. 132 # [1] Until v0.18 at least 133 set +e 134 # Get absolute path to the mapping file because resolving the relative path is tricky, given 135 # how Bazel locates the toolchain files. 136 MAPPING_FILE=$(realpath $(dirname ${BASH_SOURCE[0]}))"/IWYU_mapping.imp" 137 # IWYU always outputs something to stderr, which can be noisy if everything is fixed. 138 # Otherwise, we send the exact same arguments to include-what-you-use that we would for 139 # regular compilation with clang. 140 # We always allow SkTypes.h because it sets some defines that later #ifdefs use and IWYU is 141 # not consistent with detecting that. 142 external/clang_linux_amd64/bin/include-what-you-use $@ \ 143 -Xiwyu --keep="include/core/SkTypes.h" \ 144 -Xiwyu --keep="include/private/base/SkDebug.h" \ 145 -Xiwyu --no_default_mappings \ 146 -Xiwyu --error=3 \ 147 -Xiwyu --mapping_file=$MAPPING_FILE 2>/dev/null 148 # IWYU returns 0 if everything looks good. It returns some other non-zero exit code otherwise. 149 if [ $? -eq 0 ]; then 150 # The expected .d file is the third argument. Bazel expects this file to be created, even 151 # if it is empty. We don't really need to create this file or compile the target since 152 # we will be skipping linking anyway and not using the output for real. 153 touch $3 154 # The expected .o file is the last argument passed into clang. Make sure this file exists 155 # or Bazel validation will fail 156 touch ${!#} 157 exit 0 # keep the build going 158 else 159 # Run IWYU again, but this time display the output. Then return non-zero to fail the build. 160 # These flags are a little different, but only in ways that affect what was displayed, not the 161 # analysis. If we aren't sure why IWYU wants to include something, try changing verbose to 3. 162 external/clang_linux_amd64/bin/include-what-you-use $@ \ 163 -Xiwyu --keep="include/core/SkTypes.h" \ 164 -Xiwyu --keep="include/private/base/SkDebug.h" \ 165 -Xiwyu --no_default_mappings \ 166 -Xiwyu --mapping_file=$MAPPING_FILE -Xiwyu --no_comments \ 167 -Xiwyu --quoted_includes_first -Xiwyu --verbose=3 168 exit 1 # fail the build 169 fi 170fi 171