diff --git a/llvm-build/libcxx_patch/script/build_patch.sh b/llvm-build/libcxx_patch/script/build_patch.sh new file mode 100755 index 0000000000000000000000000000000000000000..44863f92461fe9ad2bf4d8d784e2ef756c791909 --- /dev/null +++ b/llvm-build/libcxx_patch/script/build_patch.sh @@ -0,0 +1,81 @@ +#!/bin/sh +# Copyright (C) 2025 Huawei Device Co., Ltd. +# +# 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. + +# usage build_patch.sh +# check the args +if [ $# -ne 1 ]; then + echo "Usage: $0 " + echo "Hint: is the directory where the directory llvm-project is built." + echo "Example: $0 /home/ohos/workplace/ohos/llvm-toolchain/out" + exit 1 +fi + +# check the install path +# there should be a directory named llvm in the install path +# and the llvm directory should contain the bin/clang file +LLVM_OUT_PATH=$1 +if [ ! -d $LLVM_OUT_PATH/llvm-install ]; then + echo "Error: The LLVM_OUT_PATH: $LLVM_OUT_PATH does not a legal path." + echo "Hint: is the directory where the directory llvm-project is built." + exit 1 +fi +echo "LLVM_OUT_PATH: $LLVM_OUT_PATH" + +BUILD_PATH=/tmp/libcxx_patch_build_$(($RANDOM + $(date +%s))) +echo "Building the patch..." +echo "BUILD_PATH: $BUILD_PATH" + +# libc++.so +echo "Copying libc++.so..." +mkdir -p $BUILD_PATH/patch/llvm/system_libcxx/lib/aarch64-linux-ohos +cp $LLVM_OUT_PATH/llvm-install/lib/aarch64-linux-ohos/libc++.so $BUILD_PATH/patch/llvm/system_libcxx/lib/aarch64-linux-ohos/ + +# __config_site +echo "Copying __config_site..." +mkdir -p $BUILD_PATH/patch/llvm/system_libcxx/include +cp $LLVM_OUT_PATH/llvm-install/include/libcxx-ohos/include/c++/v1/__config_site $BUILD_PATH/patch/llvm/system_libcxx/include/ + +# ohos.toolchain.cmake +echo "Copying ohos.toolchain.cmake..." +mkdir -p $BUILD_PATH/patch/build/cmake +cp $LLVM_OUT_PATH/../build/ohos/ndk/cmake/ohos.toolchain.cmake $BUILD_PATH/patch/build/cmake/ + +echo "Patching cmake toolchain file..." +sed -i '/elseif(OHOS_STL STREQUAL c++_shared)/a\ +elseif(OHOS_STL STREQUAL system)\ + # system libcxx is only supported for aarch64-linux-ohos\ + if(OHOS_TOOLCHAIN_NAME STREQUAL aarch64-linux-ohos)\ + include_directories("${OHOS_SDK_NATIVE}/llvm/system_libcxx/include")\ + link_directories("${OHOS_SDK_NATIVE}/llvm/system_libcxx/lib/${OHOS_TOOLCHAIN_NAME}")\ + else()\ + message(FATAL_ERROR "Unsupported STL configuration: ${OHOS_STL} which is only available for aarch64-linux-ohos.")\ + endif()' $BUILD_PATH/patch/build/cmake/ohos.toolchain.cmake + +# script.sh +echo "Copying install.sh..." +mkdir -p $BUILD_PATH/script +cp -r $(dirname "$(realpath "$0")")/install.sh $BUILD_PATH/script + +# test +echo "Copying test..." +mkdir -p $BUILD_PATH/test +cp -r $(dirname "$(realpath "$0")")/../test/* $BUILD_PATH/test + +# tar the patch +echo "Taring the patch..." +tar -cf $LLVM_OUT_PATH/../packages/libcxx_patch.tar.xz -C $BUILD_PATH/.. $(basename $BUILD_PATH) + +echo "Patch built successfully." +echo "The patch is saved in $LLVM_OUT_PATH/../packages/libcxx_patch.tar.xz" diff --git a/llvm-build/libcxx_patch/script/install.sh b/llvm-build/libcxx_patch/script/install.sh new file mode 100755 index 0000000000000000000000000000000000000000..b535caff2e5f483658e46d166328aa9236974b74 --- /dev/null +++ b/llvm-build/libcxx_patch/script/install.sh @@ -0,0 +1,92 @@ +#!/bin/sh +# Copyright (C) 2025 Huawei Device Co., Ltd. +# +# 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. + +# usage install.sh +# check the args +if [ $# -ne 1 ]; then + echo "Usage: $0 " + echo "Hint: is the directory where the directory llvm is located." + echo "Example: $0 /home/ohos/command-line-tools/sdk/default/openharmony/native" + exit 1 +fi + +# check the install path +# there should be a directory named llvm in the install path +# and the llvm directory should contain the bin/clang file +INSTALL_PATH=$1 +if [ ! -f $INSTALL_PATH/llvm/bin/clang ]; then + echo "Error: The INSTALL_PATH: $INSTALL_PATH does not a legal path." + echo "Hint: is the directory where the directory llvm is located." + exit 1 +fi +echo "INSTALL_PATH: $INSTALL_PATH" + +# apply the patch +echo "Applying the patch..." +SCRIPT_PATH=$(dirname "$(realpath "$0")") +cp -rf $SCRIPT_PATH/../patch/* $INSTALL_PATH +echo "Patch applied." + +# testing the patch +echo "Testing the patch..." + +# find cmake +CMAKE=$INSTALL_PATH/build-tools/cmake/bin/cmake +if [ ! -f $CMAKE ]; then + echo "Error: The cmake is not found in the path: $CMAKE" + exit 1 +else + echo "CMAKE: $CMAKE" +fi + +# find llvm-readelf +READELF=$INSTALL_PATH/llvm/bin/llvm-readelf +if [ ! -f $READELF ]; then + echo "Error: The llvm-readelf is not found in the path: $READELF" + exit 1 +else + echo "READELF: $READELF" +fi + +# == test c++_shared == +# build the test +BUILD_PATH_SHARED=/tmp/libcxx_patch_c++_shared_$(($RANDOM + $(date +%s))) +echo "Testing OHOS_STL=c++_shared..." +$CMAKE -DOHOS_STL=c++_shared -DCMAKE_TOOLCHAIN_FILE=$INSTALL_PATH/build/cmake/ohos.toolchain.cmake -B $BUILD_PATH_SHARED -S $SCRIPT_PATH/../test > /dev/null +$CMAKE --build $BUILD_PATH_SHARED > /dev/null +# check the shared library using readelf +$READELF -d $BUILD_PATH_SHARED/test | grep libc++_shared.so +if [ $? -ne 0 ]; then + echo "Error: Test failed: The libc++_shared.so is not linked." + exit 1 +fi + +# == test system == +# build the test + +BUILD_PATH_SYSTEM=/tmp/libcxx_patch_c++_system_$(($RANDOM + $(date +%s))) +echo "Testing OHOS_STL=system..." +$CMAKE -DOHOS_STL=system -DCMAKE_TOOLCHAIN_FILE=$INSTALL_PATH/build/cmake/ohos.toolchain.cmake -B $BUILD_PATH_SYSTEM -S $SCRIPT_PATH/../test > /dev/null +$CMAKE --build $BUILD_PATH_SYSTEM > /dev/null +# check the shared library using readelf +$READELF -d $BUILD_PATH_SYSTEM/test | grep libc++.so +if [ $? -ne 0 ]; then + echo "Error: Test failed: The libc++.so is not linked." + exit 1 +fi + +echo "All tests passed." +echo "Patch installed successfully." + diff --git a/llvm-build/libcxx_patch/test/CMakeLists.txt b/llvm-build/libcxx_patch/test/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..a58165fed4282a400c51404b563af310033ac8d1 --- /dev/null +++ b/llvm-build/libcxx_patch/test/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright (C) 2025 Huawei Device Co., Ltd. +# +# 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. + +cmake_minimum_required(VERSION 3.16) +project(ndk-patch-test) + +set(OHOS_ARCH arm64-v8a) +set(OHOS_PLATFORM OHOS) + +add_compile_options(-Wno-unused-command-line-argument) + +add_executable(test ./src/main.cpp) \ No newline at end of file diff --git a/llvm-build/libcxx_patch/test/src/main.cpp b/llvm-build/libcxx_patch/test/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e01c3de431f5d61355183674c07f8b2e5c4adc0 --- /dev/null +++ b/llvm-build/libcxx_patch/test/src/main.cpp @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * 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. + */ + +#include + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} \ No newline at end of file