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