. $topsrcdir/mozconfig-[% IF c("var/macos"); GET 'macos'; ELSE; GET c("var/osname"); END; %][% IF c("var/asan") %]-asan[% END %]

[% IF c("var/linux") -%]
  # We want to build with clang now and point to the GCC toolchain until #29041 is
  # fixed. We explicitly need to define the host compiler as well as for some
  # reason the gcc-toolchain argument does not get passed along otherwise.
  CC="clang --gcc-toolchain=/var/tmp/dist/gcc"
  CXX="clang++ --gcc-toolchain=/var/tmp/dist/gcc"
  HOST_CC=$CC
  HOST_CXX=$CXX

  export BINDGEN_CFLAGS='--gcc-toolchain=/var/tmp/dist/gcc'

  # set LDFLAGS for Full RELRO
  export LDFLAGS="-Wl,-z,relro -Wl,-z,now"

  [% IF c("var/linux-cross") -%]
    export LDFLAGS="-L/var/tmp/dist/gcc/[% c("var/crosstarget") %]/lib $LDFLAGS"
  [% END -%]
[% END -%]

[% IF c("var/windows") -%]
  CROSS_COMPILE=1

  HOST_CC="clang"
  HOST_CXX="clang++"
  CC="[% c('arch') %]-w64-mingw32-clang"
  CXX="[% c('arch') %]-w64-mingw32-clang++"
  CFLAGS="-include _mingw.h"
  CXXFLAGS="-fms-extensions -include _mingw.h"
  AR=llvm-ar
  RANLIB=llvm-ranlib

  clang_path=/var/tmp/dist/mingw-w64-clang

  # For Stylo
  BINDGEN_CFLAGS="-I$clang_path/[% c('arch') %]-w64-mingw32/include/c++/v1 -I$clang_path/[% c('arch') %]-w64-mingw32/include"
[% END -%]

[% IF c("var/macos") -%]
  TOOLCHAIN_DIR=/var/tmp/dist/macosx-toolchain

  # ld needs libLTO.so from llvm
  mk_add_options "export LD_LIBRARY_PATH=$TOOLCHAIN_DIR/clang/lib"

  CROSS_CCTOOLS_PATH=$TOOLCHAIN_DIR/cctools
  CROSS_SYSROOT=$TOOLCHAIN_DIR/MacOSX[% pc('macosx-toolchain', 'version') %].sdk
  CROSS_PRIVATE_FRAMEWORKS=$CROSS_SYSROOT/System/Library/PrivateFrameworks
  HARDENING_FLAGS="-Werror=format -Werror=format-security -fstack-protector-strong -D_FORTIFY_SOURCE=2"
  FLAGS="-target [% c('var/build_target') %] -B $CROSS_CCTOOLS_PATH/bin -isysroot $CROSS_SYSROOT $HARDENING_FLAGS"

  export CC="$TOOLCHAIN_DIR/clang/bin/clang $FLAGS"
  export CXX="$TOOLCHAIN_DIR/clang/bin/clang++ $FLAGS"
  export LLVMCONFIG=$TOOLCHAIN_DIR/clang/bin/llvm-config
  export LDFLAGS="-Wl,-syslibroot,$CROSS_SYSROOT -Wl,-pie"
  export BINDGEN_CFLAGS="$FLAGS"
  export TOOLCHAIN_PREFIX=$CROSS_CCTOOLS_PATH/bin/[% c('var/build_target') %]-
  export DSYMUTIL="$TOOLCHAIN_DIR/clang/bin/dsymutil"

  [% IF c("var/dev_artifacts") -%]
    export MKFSHFS=/var/tmp/dist/hfsplus-tools/newfs_hfs
    export DMG_TOOL=/var/tmp/dist/libdmg-hfsplus/dmg
    export HFS_TOOL=/var/tmp/dist/libdmg-hfsplus/hfsplus
  [% END -%]

  export HOST_CFLAGS="-g"
  export HOST_CXXFLAGS="-g"
  export HOST_LDFLAGS="-g"

  ac_add_options --target=[% c('var/build_target') %]
  export MACOS_SDK_DIR=$CROSS_SYSROOT
  export MACOS_PRIVATE_FRAMEWORKS_DIR=$CROSS_PRIVATE_FRAMEWORKS
  [% IF c("var/macos-aarch64") -%]
    # As of Clang 13, the default is -mcpu=apple-m1 when using a aarch64-apple-macos target,
    # but we're using apple64-apple-darwin, which defaults to -mcpu=apple-a7, which disables
    # a bunch of # performance-enabling CPU features.
    # TODO: We'll want to switch to aarch64-apple-macos eventually.
    export CFLAGS="$CFLAGS -mcpu=apple-m1"
    export CXXFLAGS="$CXXFLAGS -mcpu=apple-m1"
  [% END -%]
[% END -%]

ac_add_options --[% IF c("var/updater_enabled") %]enable[% ELSE %]disable[% END %]-updater
ac_add_options --[% IF c("var/updater_enabled") %]enable[% ELSE %]disable[% END %]-base-browser-update
[% IF c("var/override_updater_url") -%]
  ac_add_options --with-updater-url=[% c("var/override_updater_url") %]
[% ELSIF c("var/updater_url")  -%]
  ac_add_options --with-updater-url=[% c("var/updater_url") %]
[% END -%]

[% IF c("var/dev_artifacts") -%]
  ac_add_options --enable-tests
[% END -%]

# Necessary for package-tests step which expects dist/bin/jsapi-tests to exist.
# This is added regardless of --enable-tests, because it's also a useful debugging tool.
ac_add_options --enable-js-shell

mk_add_options MOZ_PARALLEL_BUILD=[% c("num_procs") %]

export MOZ_APP_REMOTINGNAME="[% c('var/display_name') %]"

export MAR_CHANNEL_ID="[% c('var/mar_channel_id') %]"
export ACCEPTED_MAR_CHANNEL_IDS="[% c('var/mar_channel_id') %]"

export MOZ_INCLUDE_SOURCE_INFO=1
export MOZ_SOURCE_REPO="[% c('var/gitlab_project') %]"
export MOZ_SOURCE_CHANGESET=[% c("var/git_commit") %]
