Skip to content

Commit

Permalink
Add cmake min version
Browse files Browse the repository at this point in the history
Better run target selection
  • Loading branch information
andystanton committed Aug 14, 2024
1 parent 26de513 commit 649db9d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
24 changes: 21 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ endef

project_name:=?
project_lang:=?
cmake_min_version:=?
type:=debug
generator:=$(shell if [ -x "$$(which ninja)" ]; then echo "Ninja"; else echo "Unix Makefiles"; fi)
build_path:=$(shell if [ "$(call to_lower_case,$(generator))" = "xcode" ]; then echo "xcode-build"; else echo "cmake-build-$(call to_lower_case,$(type))"; fi)
Expand All @@ -54,6 +55,9 @@ override cmake_build_type_arg:=$(call find_in_list,$(type),$(valid_types))
override cmake_generator_arg:=$(call find_in_list,$(generator),$(valid_generators))
override is_debug:=$(call exists_in_list,$(type),$(debug_types))

override default_cmake_min_version:=3.5
override default_project_lang:=c

# =============================================================================
# Utility targets
# =============================================================================
Expand Down Expand Up @@ -134,11 +138,25 @@ init:
if [ "$$project_name" = "?" ]; then \
read -p "Enter project name: " project_name; \
fi; \
if [ -z "$$project_name" ]; then \
echo "Project name required" >&2; \
exit 1; \
fi; \
project_lang=$(project_lang); \
if [ "$$project_lang" = "?" ]; then \
read -p "Enter project language (c|cpp): " project_lang; \
read -p "Enter project language (c|cpp): (default: $(default_project_lang)) " project_lang; \
fi; \
if [ -z "$$project_lang" ]; then \
project_lang="$(default_project_lang)"; \
fi; \
project_lang=$$(echo $$project_lang | tr '[:upper:]' '[:lower:]' | sed -E -e 's/^(c\+\+|cxx)$$/cpp/'); \
cmake_min_version=$(cmake_min_version); \
if [ "$$cmake_min_version" = "?" ]; then \
read -p "Enter CMake minimum version: (default: $(default_cmake_min_version)) " cmake_min_version; \
fi; \
if [ -z "$$cmake_min_version" ]; then \
cmake_min_version="$(default_cmake_min_version)"; \
fi; \
if [ "$$project_lang" = "c" ]; then \
echo "#include \"stdio.h\"\n\nint main() {\n\tprintf(\"%s\", \"hello, world!\");\n\treturn 0;\n}\n" >$$project_name.$$project_lang; \
elif [ "$$project_lang" = "cpp" ]; then \
Expand All @@ -147,7 +165,7 @@ init:
echo "Unsupported language type" >&2; \
exit 1; \
fi; \
echo "project($$project_name)\n\nadd_executable($$project_name $$project_name.$$project_lang)\n\n" >CMakeLists.txt; \
echo "cmake_minimum_required(VERSION $$cmake_min_version)\n\nproject($$project_name)\n\nadd_executable($$project_name $$project_name.$$project_lang)\n\n" >CMakeLists.txt; \
fi;

#: Configure the project for the selected type and generator
Expand Down Expand Up @@ -181,6 +199,7 @@ endif
run: build
@app_name=$$(cmake -S . -B "$(build_path)" --trace 2>&1 \
| egrep -i add_executable \
| egrep -v -i '^CMake Warning ' \
| sort -r \
| head -n 1 \
| sed -E 's/.*add_executable.[[:space:]]*([A-Za-z0-9\\"_\.+-]+)[[:space:]].*/\1/'); \
Expand Down Expand Up @@ -212,4 +231,3 @@ list-variables:

list-targets:
@egrep "^[A-Za-z0-9_-]+\:([^\=]|$$)" $(lastword $(MAKEFILE_LIST)) | sed -E 's/(.*):.*/\1/g' | sort

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ make generator=Xcode type=release run
Initialise a skeleton CMake project with an example C source file:

```sh
make project_name=foo project_lang=c init
make project_name=foo project_lang=c cmake_min_version=3.5 init
```

## Targets
Expand Down Expand Up @@ -55,7 +55,8 @@ make project_name=foo project_lang=c init
### Variables that apply only to the init target

- `project_name`: if supplied then the init target will not prompt for a project name.
- `project_lang`: if supplied then the init target will not prompt for a project language. Valid languages are 'c' and 'cpp'.
- `project_lang`: if supplied then the init target will not prompt for a project language. Valid languages are 'c' and 'cpp'. Default value is 'c'.
- `cmake_min_version`: if supplied then the init target will not prompt for a CMake minimum version. Default value is 3.5.

## Dependencies

Expand Down

0 comments on commit 649db9d

Please sign in to comment.