2025-05-26 19:57:26 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
# Convenience script to create a C/C++ NeteBeans project.
|
|
|
|
|
# Copyright (C) 26.5.2025 Benny Lyons benny.lyons@gmx.net
|
|
|
|
|
#
|
|
|
|
|
# nb_create.sh is free software; you can redistribute it and/or modify it under
|
|
|
|
|
# the terms of the GNU General Public License as published by the Free Software
|
|
|
|
|
# Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
|
# version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
|
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
|
# this program; if not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Default settings
|
|
|
|
|
#
|
|
|
|
|
# Default Git repository containing Makefile, NetBeans project file, ...
|
|
|
|
|
# Override this using -r my_repo.git; or reset this
|
2025-05-30 16:16:30 +02:00
|
|
|
DEFAULT_REPO="https://codeberg.org/mebenn/netbeans_generic.git/"
|
2025-05-26 19:57:26 +02:00
|
|
|
|
|
|
|
|
this_script=$(basename -- $0)
|
|
|
|
|
USE_PROGRAMME=git
|
|
|
|
|
|
|
|
|
|
help() {
|
2025-05-30 17:25:28 +02:00
|
|
|
cat <<EOF
|
2025-05-26 19:57:26 +02:00
|
|
|
|
|
|
|
|
Usage: $this_script [OPTIONS] params
|
2025-06-14 14:20:51 +02:00
|
|
|
Convenience script to create a C/C++ NetBeans projects.
|
|
|
|
|
|
|
|
|
|
Create a generic NetBeans projec using a Git repo.
|
|
|
|
|
Project name and file containing main are by default
|
|
|
|
|
the name of the current directory. These can be changed
|
|
|
|
|
by using command line options.
|
|
|
|
|
|
|
|
|
|
The Makefile currently has these targets:
|
|
|
|
|
debug:
|
|
|
|
|
check1: cppcheck static analysis
|
|
|
|
|
check2: clang-tidy
|
2025-05-26 19:57:26 +02:00
|
|
|
Options:
|
2025-05-28 12:23:08 +02:00
|
|
|
-d [--debug]
|
2025-06-14 14:20:51 +02:00
|
|
|
Add extra debugging infos for this script
|
2025-05-26 19:57:26 +02:00
|
|
|
-h [--help]
|
|
|
|
|
print this help
|
|
|
|
|
-m [--main] <name_of_file_containing_main>
|
|
|
|
|
The name of the file that contains the main function
|
|
|
|
|
default: name of the project
|
|
|
|
|
-p [--project] <propject_name>
|
|
|
|
|
Provide a name of the project instead of the default
|
|
|
|
|
default: name of the current directory
|
|
|
|
|
-r [--repo] <git_repo>
|
|
|
|
|
The Git repo containing the NetBeans generic C/C++ project
|
2025-06-14 14:20:51 +02:00
|
|
|
Default: https://codeberg.org/mebenn/netbeans_generic.git
|
2025-05-26 19:57:26 +02:00
|
|
|
Licence:
|
|
|
|
|
$this_script Copyright (C) 2025 Benny Lyons.
|
|
|
|
|
GNU GPL2 or later; see LICENCE distributed with this file for more details.
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-05-30 10:30:10 +02:00
|
|
|
if_no_dir_then_exit()
|
|
|
|
|
{
|
|
|
|
|
exist_dir=$1
|
|
|
|
|
message=$2
|
|
|
|
|
|
|
|
|
|
if [ ! -d $exist_dir ]; then
|
|
|
|
|
echo "ERROR: Can't find: "$exist_dir
|
|
|
|
|
echo " $message"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if_no_file_then_exit()
|
|
|
|
|
{
|
|
|
|
|
exist_file=$1
|
|
|
|
|
message=$2
|
|
|
|
|
|
|
|
|
|
if [ ! -f $exist_file ]; then
|
|
|
|
|
echo "ERROR: Can't find: "$exist_file
|
|
|
|
|
echo " $message"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-05-26 19:57:26 +02:00
|
|
|
main()
|
|
|
|
|
{
|
2025-05-30 10:30:10 +02:00
|
|
|
OPT_DEBUG=False # By default, debugging is off
|
2025-06-08 15:47:50 +02:00
|
|
|
|
2025-05-28 12:23:08 +02:00
|
|
|
OPT_ALL="$(getopt -o dhm:p:r: -l debug,help,main:,project:,repo: --name "$0" -- "$@")"
|
2025-05-26 19:57:26 +02:00
|
|
|
eval set -- "$OPT_ALL"
|
|
|
|
|
|
|
|
|
|
while true ; do
|
|
|
|
|
case $1 in
|
2025-05-28 12:23:08 +02:00
|
|
|
-d | --debug)
|
|
|
|
|
OPT_DEBUG=True
|
2025-06-08 15:47:50 +02:00
|
|
|
shift 1
|
2025-05-28 12:23:08 +02:00
|
|
|
;;
|
|
|
|
|
-h | --help) # Call help
|
2025-05-26 19:57:26 +02:00
|
|
|
help
|
|
|
|
|
exit 0
|
|
|
|
|
;;
|
2025-05-28 12:23:08 +02:00
|
|
|
-m | --main)
|
|
|
|
|
OPT_MAIN=$2
|
2025-06-08 15:47:50 +02:00
|
|
|
shift 2
|
2025-05-26 19:57:26 +02:00
|
|
|
;;
|
2025-05-28 12:23:08 +02:00
|
|
|
-p | --project)
|
|
|
|
|
OPT_PROJECT=$2
|
2025-06-08 15:47:50 +02:00
|
|
|
shift 2
|
2025-05-28 12:23:08 +02:00
|
|
|
;;
|
|
|
|
|
-r | --repo )
|
|
|
|
|
OPT_REPO=$2
|
2025-06-08 15:47:50 +02:00
|
|
|
shift 2
|
2025-05-28 12:23:08 +02:00
|
|
|
;;
|
|
|
|
|
--)
|
|
|
|
|
shift
|
|
|
|
|
break
|
2025-05-26 19:57:26 +02:00
|
|
|
;;
|
2025-05-28 12:23:08 +02:00
|
|
|
*)
|
2025-06-08 15:47:50 +02:00
|
|
|
echo "Invalid argument"
|
2025-05-28 12:23:08 +02:00
|
|
|
exit 1
|
|
|
|
|
;;
|
2025-05-26 19:57:26 +02:00
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2025-06-08 15:47:50 +02:00
|
|
|
|
2025-05-28 12:23:08 +02:00
|
|
|
if [[ $OPT_DEBUG == "True" ]]; then
|
|
|
|
|
trap 'echo "At line $LINENO: $BASH_COMMAND"' DEBUG
|
|
|
|
|
fi
|
|
|
|
|
|
2025-05-26 19:57:26 +02:00
|
|
|
if ! command -v "${USE_PROGRAMME}" > /dev/null; then
|
|
|
|
|
echo "ERROR: cannot find " $USE_PROGRAMME
|
|
|
|
|
echo " You must install "$USE_PROGRAMME
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pwd="$PWD"
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Set defaults
|
|
|
|
|
#
|
|
|
|
|
if [ -z $OPT_PROJECT ]; then
|
|
|
|
|
PROJECT=$(basename $pwd)
|
|
|
|
|
else
|
|
|
|
|
PROJECT="$OPT_PROJECT"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -z $OPT_MAIN ]; then
|
|
|
|
|
MAIN="$PROJECT"
|
|
|
|
|
else
|
|
|
|
|
MAIN="$OPT_MAIN"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -z $OPT_REPO ]; then
|
|
|
|
|
REPO=$DEFAULT_REPO
|
|
|
|
|
else
|
|
|
|
|
REPO="$OPT_REPO"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Do
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
cd $pwd
|
|
|
|
|
|
2025-05-30 16:16:30 +02:00
|
|
|
[ ! -d ".git" ] && echo "Checking out repo..." && $(git clone $REPO .)
|
|
|
|
|
[ ! -d ".git" ] && echo "ERROR: not a Git repo" && exit 1
|
|
|
|
|
|
2025-05-30 10:30:10 +02:00
|
|
|
if_no_dir_then_exit $pwd/nbproject "Not a NetBeans project"
|
|
|
|
|
if_no_file_then_exit $pwd/nbproject/project.xml "Netbeans project without a configuration file!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if [ ! -f "compile_commands.json" ]; then
|
|
|
|
|
echo "WARNING: ./compile_commands.json not found"
|
|
|
|
|
echo " Netbeans project without a compilation database"
|
|
|
|
|
echo " Target check2 will not work"
|
|
|
|
|
echo " Generate compilation database: bear -- make"
|
|
|
|
|
else
|
|
|
|
|
replace="directory"
|
|
|
|
|
replacement="$pwd"
|
|
|
|
|
sed -i "s%^\s*\"$replace\"\:.*%\"directory\"\:\ \"$replacement\"\,%g" compile_commands.json
|
|
|
|
|
|
|
|
|
|
replace="file"
|
|
|
|
|
replacement="$pwd""/src/${MAIN}.cpp"
|
|
|
|
|
sed -i "s%^\s*\"$replace\"\:.*%\"file\"\:\ \"$replacement\"\,%g" compile_commands.json
|
|
|
|
|
|
|
|
|
|
replace="output"
|
|
|
|
|
replacement="$pwd""/obj/${MAIN}.obj"
|
2025-06-14 13:49:24 +02:00
|
|
|
sed -i "s%^\s*\"$replace\"\:.*%\"output\"\:\ \"$replacement\"\,%g" compile_commands.json
|
|
|
|
|
|
|
|
|
|
sed -i "/obj\/myapp.o/c\"obj\/${MAIN}.o\"\," compile_commands.json
|
|
|
|
|
sed -i "/src\/myapp.cpp/c\"src\/${MAIN}.cpp\"\," compile_commands.json
|
2025-05-28 12:23:08 +02:00
|
|
|
fi
|
|
|
|
|
|
2025-05-30 10:30:10 +02:00
|
|
|
|
2025-05-30 16:16:30 +02:00
|
|
|
cd $pwd/nbproject
|
2025-05-30 17:34:35 +02:00
|
|
|
sed -i -e "s/<name>.*<\/name>/<name>$PROJECT<\/name>/g" project.xml
|
2025-05-30 16:16:30 +02:00
|
|
|
|
|
|
|
|
cd "$pwd"/src
|
|
|
|
|
if [ -f ./myapp.cpp ]; then
|
|
|
|
|
mv myapp.cpp "$MAIN".cpp
|
|
|
|
|
fi
|
2025-05-26 19:57:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main $@
|