From 9b874ad9b9e76ab9e04072c7df812a76a42dc1aa Mon Sep 17 00:00:00 2001 From: Zhuo Yang Date: Wed, 11 Sep 2024 21:19:50 +0800 Subject: [PATCH] refactor: re-implement the project, which should be available for both org and com version of OpenFOAM --- .gitee/ISSUE_TEMPLATE.zh-CN.md | 13 --- .gitee/PULL_REQUEST_TEMPLATE.zh-CN.md | 11 -- README.md | 29 +++--- README.zh_CN.md | 10 +- install | 4 +- ofCmakeConfig | 140 ++++---------------------- wmakelog2cmakelists.py | 125 +++++++++++++++++++++++ 7 files changed, 168 insertions(+), 164 deletions(-) delete mode 100644 .gitee/ISSUE_TEMPLATE.zh-CN.md delete mode 100644 .gitee/PULL_REQUEST_TEMPLATE.zh-CN.md create mode 100644 wmakelog2cmakelists.py diff --git a/.gitee/ISSUE_TEMPLATE.zh-CN.md b/.gitee/ISSUE_TEMPLATE.zh-CN.md deleted file mode 100644 index f09d98d..0000000 --- a/.gitee/ISSUE_TEMPLATE.zh-CN.md +++ /dev/null @@ -1,13 +0,0 @@ -### 该问题是怎么引起的? - - - -### 重现步骤 - - - -### 报错信息 - - - - diff --git a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md b/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md deleted file mode 100644 index d470fe2..0000000 --- a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md +++ /dev/null @@ -1,11 +0,0 @@ -### 相关的Issue - - -### 原因(目的、解决的问题等) - - -### 描述(做了什么,变更了什么) - - -### 测试用例(新增、改动、可能影响的功能) - diff --git a/README.md b/README.md index ec9d98b..9c7a386 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,24 @@ # of_cmake_config [中文](./README.zh_CN.md) | [English](./README.md) -#### Description -This project is used to generate CMakeLists.txt for OpenFOAM project. +#### Introduction +This project is used to generate `CMakeLists.txt` for OpenFOAM projects. -#### Installation -0. Activate OpenFOAM environment: - - Using alias `of2012clang` or `of2012clangdebug` - - source directly: `source $HOME/OpenFOAM/OpenFOAM-v2012/etc/bashrc WM_COMPILER=Clang ...` -1. Get this project: `git clone https://github.com/zhyang-dev/of_cmake_config.git` +#### Installation Guide +0. Activate the required OpenFOAM environment (otherwise the installation will fail). +1. Obtain the project source code: `git clone https://github.com/zhyang-dev/of_cmake_config.git` 2. Install: `cd of_cmake_config && ./install` -#### Instructions +#### Usage Instructions -1. Activate OpenFOAM environment -2. In project root path, - - run `ofCmakeConfig` to generate `CMakeLists.txt` - - or run `occ`, which is a wrapper of `ofCmakeConfig` script, to generate `CMakeLists.txt` and `compile_commands.json`. +0. Activate the OpenFOAM environment. +1. In the project's root directory: + - Run `ofCmakeConfig` to generate `CMakeLists.txt`. + - Or run `occ`, which will execute `ofCmakeConfig` and call `cmake -B build`, eventually generating `compile_commands.json` in the build directory. -![video demo](demo/occ_demo.gif) \ No newline at end of file +#### Testing Environment +- `vscode` + `clangd` +- `vim` + `coc-clangd` + +The following demonstrates the second scenario based on the icoFoam case. +![Video Demo](demo/occ_demo.gif) \ No newline at end of file diff --git a/README.zh_CN.md b/README.zh_CN.md index cafcf3a..9ffb671 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -5,9 +5,7 @@ 本项目用于生成OpenFOAM项目的CMakeLists.txt #### 安装教程 -0. 激活OpenFOAM环境: - - 使用别名 `of2012clang` or `of2012clangdebug` - - 直接source: `source $HOME/OpenFOAM/OpenFOAM-v2012/etc/bashrc WM_COMPILER=Clang ...` +0. 激活需要的OpenFOAM环境(否则会安装失败) 1. 获取该项目源码:`git clone https://github.com/zhyang-dev/of_cmake_config.git` 2. 安装:`cd of_cmake_config && ./install` @@ -15,12 +13,12 @@ 0. 激活OpenFOAM环境 1. 在项目根目录下, - - 运行 `ofCmakeConfig`,生成`CMakeLists.txt` - - 或运行`occ`,其包装了`ofCmakeConfig`,除了生成`CMakeLists.txt`,还会调用cmake,生成`compile_commands.json`。 - + - 运行 `ofCmakeConfig`,可以生成`CMakeLists.txt` + - 或运行`occ`,它会执行`ofCmakeConfig`,并调用`cmake -B build`,最终在build中会生成`compile_commands.json`。 #### 测试环境 - `vscode` + `clangd` - `vim` + `coc-clangd` +以下基于icoFoam算例,演示第二种情况。 ![视频演示](demo/occ_demo.gif) diff --git a/install b/install index 5694848..0b2f341 100755 --- a/install +++ b/install @@ -3,12 +3,10 @@ objDir=$WM_PROJECT_DIR/wmake if [ -d $objDir ] then - cwd=`pwd` chmod +x ./ofCmakeConfig chmod +x ./occ rm -f $objDir/ofCmakeConfig $objDir/occ - ln -s $cwd/ofCmakeConfig $objDir/ - ln -s $cwd/occ $objDir/ + cp ofCmakeConfig occ wmakelog2cmakelists.py $objDir/ echo "installed ofCmakeConfig" else echo "please active OpenFOAM environment" diff --git a/ofCmakeConfig b/ofCmakeConfig index 190f233..532c416 100755 --- a/ofCmakeConfig +++ b/ofCmakeConfig @@ -1,127 +1,31 @@ #!/bin/bash -Script="${0##*/}" # Need 'Script' for wmakeFunctions messages -scriptsDir="${0%/*}"/scripts # wmake/scripts directory -. "$scriptsDir"/wmakeFunctions # Source wmake functions - -printInfo() { - if [ -f "$WM_DIR"/makefiles/info ] - then - make --no-print-directory -f "$WM_DIR"/makefiles/info "$@" - else - echo "OpenFOAM environment not set?" 1>&2 - return 1 - fi -} - -if [ -z "$objectsDir" ] +if [ ! -f "log.wmake" ] then - objectsDir="$MakeDir/$WM_OPTIONS" -fi -mkdir -p "$objectsDir" - -make -s -f "$WM_DIR"/makefiles/files MAKE_DIR="$MakeDir" OBJECTS_DIR="$objectsDir" "$objectsDir"/options -make -s -f "$WM_DIR"/makefiles/files MAKE_DIR="$MakeDir" OBJECTS_DIR="$objectsDir" -s=`make --dry-run -f "$WM_DIR"/makefiles/general MAKE_DIR="$MakeDir" OBJECTS_DIR="$objectsDir" $targetType` - -if [ $WM_COMPILE_OPTION = "Debug" ]; then - echo $s + echo "wmake > log.wmake" + wmake $@ >log.wmake 2>&1 fi -link_flags_extra=`echo $s | grep -oP '(?<=Xlinker)(.*?)(?=Make)'` -link_flags_extra="-Xlinker $link_flags_extra" +# Find the Python executable in the system +PYTHON_EXEC=$(which python3) -OF_compile_flags=`printInfo "cxxflags"` -OF_compile_flags="$OF_compile_flags -iquote." -OF_link_flags="$OF_compile_flags $link_flags_extra" - -OF_project_name=`cat Make/files | grep EXE | grep -oP '(?<=/).*$'` -OF_typestr="add_executable" -if [ -z $OF_project_name ] -then - OF_project_name=`cat Make/files | grep LIB | grep -oP '(?<=/).*$'` - OF_typestr="add_library" +# Check if python3 is found +if [ -z "$PYTHON_EXEC" ]; then + echo "python3 not found" + exit 1 fi -OF_sources=`cat Make/files | grep -oP '.*\.C'` -if [ $OF_typestr = "add_executable" ] -then - OF_typestr="$OF_typestr(\${project_name} \${OF_sources})" +# Get the Python version +PYTHON_VERSION=$($PYTHON_EXEC -c 'import sys; print(".".join(map(str, sys.version_info[:3])))') + +# Split the version number +IFS='.' read -r -a version_parts <<< "$PYTHON_VERSION" + +# Compare major and minor versions +if [ "${version_parts[0]}" -gt 3 ] || { [ "${version_parts[0]}" -eq 3 ] && [ "${version_parts[1]}" -ge 6 ]; }; then + script_dir=$(cd "$(dirname "$0")" && pwd) + $PYTHON_EXEC $script_dir/wmakelog2cmakelists.py else - OF_typestr="$OF_typestr(\${project_name} SHARED \${OF_sources})" -fi - -str_inc=`echo $s | sed 's/ /\n/g' | grep -E '^-I' | sort | uniq |grep -oP '(?<=-I).*'` -OF_includeDir_tmp=${str_inc/%lnInclude/} -OF_includeDir="" -for inc in $OF_includeDir_tmp -do - if [ "${inc:0:1}" != "/" ] - then - inc="\${CMAKE_SOURCE_DIR}/$inc" - fi - OF_includeDir="$OF_includeDir $inc" -done -OF_includeDir="$OF_includeDir \${CMAKE_SOURCE_DIR}/lnInclude" -OF_includeDir=`echo $OF_includeDir | sed 's/ /\n/g'` - -OF_linkLib=`echo $s | sed 's/ /\n/g' | grep -E '^-l' | sort | uniq |grep -oP '(?<=-l).*'` -OF_linkDir=$WM_PROJECT_DIR/platforms/$WM_OPTIONS/lib -OF_linkDir=`echo "$OF_linkDir $FOAM_USER_LIBBIN" | sed 's/ /\n/g'` - -# 写入文件 CMakeLists.txt -cat > CMakeLists.txt <