#
# Copyright 2015-2016, Chef Software, Inc.
# Copyright 2026, Cinc Project
# Copyright 2026, Oregon State University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name "omnibus-toolchain"
default_version "1.0.0"

license :project_license

# gnu utilities
if windows?
  dependency "cmake"
else
  dependency "libtool"
  dependency "libarchive"
  # gtar must be built before bash. bash pulls in libiconv, which
  # drops a GNU libiconv iconv.h into embedded/include. gtar's
  # configure (AM_ICONV) then detects libiconv and emits calls to
  # libiconv()/libiconv_open() in utf8.c, but autoconf does not add
  # -liconv to the link command and the build dies with "undefined
  # reference to libiconv". Building gtar first means iconv.h is not
  # in embedded/include yet, so gtar uses glibc's built-in iconv.
  dependency "gtar"
  dependency "bash"
  dependency "curl"
  dependency "make"
  dependency "makedepend"
  dependency "patch"
end

# Bundle ccache on Linux so downstream builds (and our own subsequent
# software builds inside this toolchain build) get a known-good
# ccache 4.x from embedded/bin instead of whatever the host
# happens to ship. Skipped on macOS/FreeBSD (self-hosted runners
# install ccache out of band) and Windows (no ccache use).
dependency "ccache" if linux?

dependency "cacerts"

# ruby core tools
dependency "ruby"

# Projects such as Chef Server, Supermarket, Chef Manage and Chef Backend
# use berkshelf for the build time installation of gems. Moving it to the
# toolchain avoids installing a ton of deps into our packages.
#
# chef-server and its components never built on macOS, so we only need
# berkshelf on Linux.
if linux?
  dependency "berkshelf"
end

dependency "nokogiri"

# Include helpers for build pipelines
dependency "helper-gems"

# Bundle cinc-auditor so downstream InSpec tests run on every
# build host without an extra omnitruck bootstrap step.
dependency "cinc-auditor"

build do
  env = with_standard_compiler_flags(with_embedded_path)

  # Surface the ccache binary into the public-facing bin/ directory.
  # omnibus has no automatic embedded/bin -> bin/ mirroring; each
  # wrapper that ends up in bin/ is placed there explicitly. We want
  # ccache visible at /opt/omnibus-toolchain/bin/ccache so downstream
  # builders can reference it directly without needing embedded/bin
  # on PATH.
  link "#{install_dir}/embedded/bin/ccache", "#{install_dir}/bin/ccache" if linux?

  # The mini-portile2 gem ships with some test fixture data compressed in a format Apple's notarization
  # service cannot understand. We need to delete that archive from gem cache to pass notarization.
  block "Delete test folder of mini-portile2 gem cache so downstream projects pass notarization" do
    env["VISUAL"] = "echo"
    gem_install_dir = shellout!("#{install_dir}/embedded/bin/gem environment gemdir", env: env).stdout.chomp
    remove_directory "#{gem_install_dir}/cache/mini_portile2-2.4.0.gem"
  end
end
