1#!/bin/bash
2
3# Copyright 2018 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# This script takes an existing llvm ebuild file and generate a llvm-extra
8# ebuild. The newly generated llvm-extra ebuild can be installed as a regular
9# host package.
10# The new ebuild should be generated in sys-devel/llvm-extra directory.
11# The script also copies all the files from files/ directory.
12# The generated llvm-extra ebuild is slotted so multiple instances of
13# llvm-extra ebuilds can be installed at same time.
14# The slot is derived based on the _pre<num> string in the llvm ebuild name.
15# e.g. For llvm-7.0_pre331547_p20180529-r8.ebuild, the slot will be
16# 7.0_pre331547.
17#
18# Usage:
19#  ./create_llvm_extra.sh /path/to/llvm-7.0_pre331547_p20180529-r8.ebuild
20#
21# To use the clang installed by llvm-extra, modify the CFLAGS and
22# LDFLAGS of a pckage to pass the patch of the clang binary installed by
23# the llvm-extra package.
24# e.g. append-flags -Xclang-path=/usr/llvm-extra/version/clang
25#      append-ldflags -Xclang-path=/usr/llvm-extra/version/clang
26#
27
28SCRIPT_DIR=$(realpath $(dirname "$0"))
29
30function check_cmd() {
31	if [[ "$#" -ne 1 ]]; then
32		echo "Exactly 1 argument expected"
33		echo "Usage $0 <path_to_llvm_ebuild>"
34		exit 1
35	fi
36	if [[ ! -f "$1" ]]; then
37		echo "$1 is not a file"
38		exit 1;
39	fi
40}
41
42function create_llvm_extra_ebuild() {
43	EBUILD_PREFIX=llvm-extra
44	EBUILD_DIR=$(dirname "$1")
45	EBUILD_FILE_NAME=$(basename "$1")
46	NEW_EBUILD_FILE_NAME="${EBUILD_FILE_NAME/llvm/$EBUILD_PREFIX}"
47	NEW_EBUILD_FILENAME_NO_EXT="${NEW_EBUILD_FILE_NAME%.*}"
48	NEW_EBUILD_DIR="${EBUILD_DIR}/../${EBUILD_PREFIX}"
49	NEW_EBUILD_PV="${NEW_EBUILD_FILENAME_NO_EXT#"$EBUILD_PREFIX-"}"
50	NEW_EBUILD_SLOT="${NEW_EBUILD_PV%%_p[[:digit:]]*}"
51
52	mkdir -p "${NEW_EBUILD_DIR}"
53	if [[ -d "${EBUILD_DIR}/files" ]]; then
54		cp -rf "${EBUILD_DIR}/files" "${NEW_EBUILD_DIR}"
55	fi
56
57	if [[ -f "${NEW_EBUILD_DIR}/${NEW_EBUILD_FILE_NAME}" ]]; then
58		echo "Removing existing ebuild file ${NEW_EBUILD_FILE_NAME}"
59		rm -f "${NEW_EBUILD_DIR}/${NEW_EBUILD_FILE_NAME}"
60	fi
61	# Generate the llvm-extra ebuild file.
62	"${SCRIPT_DIR}"/create_ebuild_file.py "$1" "${NEW_EBUILD_DIR}/${NEW_EBUILD_FILE_NAME}"
63	if [[ $? -ne 0 ]]; then
64		echo "Creation of ${NEW_EBUILD_DIR}/${NEW_EBUILD_FILE_NAME} failed"
65		exit 1
66	fi
67	echo "***"
68	echo "***"
69	echo "${NEW_EBUILD_DIR}/${NEW_EBUILD_FILE_NAME} has been created."
70
71	echo "***"
72	echo "Test if it builds by running \$ sudo emerge ${EBUILD_PREFIX}:${NEW_EBUILD_SLOT}"
73	echo "***"
74	echo "If it works, Go ahead and submit the newly generated ebuild"\
75	     "and any other files in ${NEW_EBUILD_DIR}."
76	echo "***"
77	echo "Don't forget to add sys-devel/${EBUILD_PREFIX}:${NEW_EBUILD_SLOT} to"\
78	     "the dependencies in virtual/target-chromium-os-sdk ebuild."
79	echo "***"
80	echo "***"
81}
82
83
84set -e
85# Sanity checks.
86check_cmd "${@}"
87# Create llvm-extra ebuild.
88create_llvm_extra_ebuild "${@}"
89