#!/bin/sh set -eu base="${TMPDIR-/tmp}/mcpu-cpp-config-order-$$" mkdir -p \ "$base/cmd-I" \ "$base/cmd-isystem" \ "$base/config-as" \ "$base/config-user" \ "$base/system/as" \ "$base/cmd-after" \ "$base/config-after" trap 'rm -rf "$base"' EXIT HUP INT TERM cat > "$base/config" < "$base/main.c" <<'EOT' #lang "as" #include #endlang EOT cat > "$base/cmd-I/same.h" <<'EOT' from_explicit_I #include_next EOT cat > "$base/cmd-isystem/same.h" <<'EOT' from_explicit_isystem #include_next EOT cat > "$base/config-as/same.h" <<'EOT' from_config_language #include_next EOT cat > "$base/config-user/same.h" <<'EOT' from_config_user #include_next EOT cat > "$base/system/as/same.h" <<'EOT' from_system_language #include_next EOT cat > "$base/system/same.h" <<'EOT' from_system_root #include_next EOT cat > "$base/cmd-after/same.h" <<'EOT' from_explicit_idirafter #include_next EOT cat > "$base/config-after/same.h" <<'EOT' from_config_after EOT # Deliberately scramble command-line option order. Semantic classes, not # argv order, define precedence. "$MCPU_CPP" --config-file "$base/config" \ -idirafter "$base/cmd-after" \ -isystem "$base/cmd-isystem" \ -I "$base/cmd-I" \ "$base/main.c" -o "$base/out" awk ' /from_explicit_I/ { a = NR } /from_explicit_isystem/ { b = NR } /from_config_language/ { c = NR } /from_config_user/ { d = NR } /from_system_language/ { e = NR } /from_system_root/ { f = NR } /from_explicit_idirafter/ { g = NR } /from_config_after/ { h = NR } END { exit !(a && a < b && b < c && c < d && d < e && e < f && f < g && g < h) } ' "$base/out" # An empty MCPU_CPP_SYSTEM_INCLUDE_PATH disables the complete configured # system tree, including the automatically derived subdirectory. cat > "$base/no-system.conf" < "$base/system-only.c" <<'EOT' #lang "as" #include #endlang EOT cat > "$base/system/as/system-only.h" <<'EOT' system_only_header EOT if "$MCPU_CPP" --config-file "$base/no-system.conf" \ "$base/system-only.c" -o "$base/no-system.out" 2>"$base/no-system.err"; then echo 'empty MCPU_CPP_SYSTEM_INCLUDE_PATH did not disable system includes' >&2 exit 1 fi # -nostdinc suppresses the configured system tree for one invocation but does # not suppress explicit -isystem or either after class. cat > "$base/nostdinc.c" <<'EOT' #lang "as" #include #endlang EOT cat > "$base/system/as/nostdinc.h" <<'EOT' wrong_configured_system EOT cat > "$base/cmd-after/nostdinc.h" <<'EOT' from_after_with_nostdinc EOT "$MCPU_CPP" --config-file "$base/config" -nostdinc \ -idirafter "$base/cmd-after" "$base/nostdinc.c" -o "$base/nostdinc.out" grep '^from_after_with_nostdinc$' "$base/nostdinc.out" >/dev/null ! grep '^wrong_configured_system$' "$base/nostdinc.out" >/dev/null