From 2fa0eb2b05f1b37111bb027d8e34a891edb91929 Mon Sep 17 00:00:00 2001 From: Sarah Faey Date: Wed, 18 Sep 2024 22:53:52 +0200 Subject: [PATCH] added template files --- .ecc/LICENSE.md | 29 +++++++++++ .ecc/README.md | 49 ++++++++++++++++++ .ecc/_manifest.lua | 4 ++ .ecc/_preload.lua | 35 +++++++++++++ .ecc/ecc.lua | 119 ++++++++++++++++++++++++++++++++++++++++++++ .vscode/launch.json | 16 ++++++ .vscode/tasks.json | 27 ++++++++++ README.md | 16 +++--- main.cpp | 6 +++ premake5.lua | 27 ++++++++++ 10 files changed, 320 insertions(+), 8 deletions(-) create mode 100644 .ecc/LICENSE.md create mode 100644 .ecc/README.md create mode 100644 .ecc/_manifest.lua create mode 100644 .ecc/_preload.lua create mode 100644 .ecc/ecc.lua create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 main.cpp create mode 100644 premake5.lua diff --git a/.ecc/LICENSE.md b/.ecc/LICENSE.md new file mode 100644 index 0000000..ae67c3a --- /dev/null +++ b/.ecc/LICENSE.md @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2022, Matvey Bystrin +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/.ecc/README.md b/.ecc/README.md new file mode 100644 index 0000000..09ab63e --- /dev/null +++ b/.ecc/README.md @@ -0,0 +1,49 @@ +# Export Compile Commands - ECC +Module implementing support for [JSON Compilation Database Format +Specification](https://clang.llvm.org/docs/JSONCompilationDatabase.html). + +This is an alternative to +[tarruda's](https://github.com/tarruda/premake-export-compile-commands) module, +which does one simple thing - generates one `compile_commands.json` file for +your project. + +Tested with clangd-13. + +## Requirements +Premake 5.0.0 or later. + +## How to use +Download premake-ecc and place it near your premake5.lua script. Then require it +from your premake script + +```lua +require "ecc/ecc" +``` + +After you simply can call: + +``` +premake5 ecc +``` + +Moldule will generate **one** `compile_commands.json` file near your main +premake script. During generation it will use the default config (the first one +you have specified in script). If you want to select specific config just pass +it's name with command line option: + +``` +premake5 --config=release ecc +``` + +Careful! `config` option case sensitive! If there is no config passed via +command line, module will choose the default one. + +Note: if you want to embed this module into your premake build follow +the [manual](https://premake.github.io/docs/Embedding-Modules/) + +## Future plans +- Add unit tests + +## Alternatives +- [export-compile-commands](https://github.com/tarruda/premake-export-compile-commands) +- [bear](https://github.com/rizsotto/Bear) diff --git a/.ecc/_manifest.lua b/.ecc/_manifest.lua new file mode 100644 index 0000000..1f70942 --- /dev/null +++ b/.ecc/_manifest.lua @@ -0,0 +1,4 @@ +return { + "_preload.lua", + "ecc.lua" +} diff --git a/.ecc/_preload.lua b/.ecc/_preload.lua new file mode 100644 index 0000000..347dada --- /dev/null +++ b/.ecc/_preload.lua @@ -0,0 +1,35 @@ + + local p = premake + + newoption { + trigger = "config", + value = "CFG", + description = "Select config for export compile_commands.json" + } + + newaction { + trigger = "ecc", + shortname = "Export compile commands", + description = "Export compile_commands.json for language server", + toolset = "gcc", + + valid_kinds = { "ConsoleApp", "WindowedApp", "StaticLib", "SharedLib" }, + valid_languages = { "C", "C++" }, + valid_tools = { + cc = { "clang", "gcc" } + }, + + onStart = function() + p.indent(" ") + end, + + execute = function() + local dir = {} + dir.location = _MAIN_SCRIPT_DIR + p.generate(dir, "compile_commands.json", p.modules.ecc.generateFile) + end + } + + return function(cfg) + return (_ACTION == "ecc") + end diff --git a/.ecc/ecc.lua b/.ecc/ecc.lua new file mode 100644 index 0000000..2646f18 --- /dev/null +++ b/.ecc/ecc.lua @@ -0,0 +1,119 @@ + + -- Include module if it is not embedded + if premake.modules.ecc == nil then + include ( "_preload.lua" ) + end + + local p = premake + local project = p.project + + p.modules.ecc = {} + local m = p.modules.ecc + + m._VERSION = "1.0.1-alpha" + + function m.generateFile() + p.push("[") + for wks in p.global.eachWorkspace() do + for prj in p.workspace.eachproject(wks) do + m.onProject(prj) + end + end + p.pop("]") + end + + function m.onProject(prj) + if project.isc(prj) or project.iscpp(prj) then + local cfg = m.getConfig(prj) + local args = m.getArguments(prj, cfg) + local files = table.shallowcopy(prj._.files) + for i,node in ipairs(files) do + local output = cfg.objdir .. "/" .. node.objname .. ".o" + local obj = path.getrelative(prj.location, output) + p.push("{") + p.push("\"arguments\": [") + m.writeArgs(args, obj, node.relpath) + p.pop("],") + p.w("\"directory\": \"%s\",", prj.location) + p.w("\"file\": \"%s\",", node.abspath) + p.w("\"output\": \"%s\"", output) + p.pop("},") + end + end + end + + function m.writeArgs(args, obj, src) + for _,arg in ipairs(args) do + p.w("\"%s\",", arg) + end + p.w("\"-c\",") + p.w("\"-o\",") + p.w("\"%s\",", obj) + p.w("\"%s\"", src) + end + + function m.getConfig(prj) + local ocfg = _OPTIONS.config + local cfg = {} + if ocfg and prj.configs[ocfg] then + cfg = prj.configs[ocfg] + else + cfg = m.defaultconfig(prj) + end + return cfg + end + + function m.getArguments(prj, cfg) + local toolset = m.getToolSet(cfg) + local args = {} + local tool = iif(project.iscpp(prj), "cxx", "cc") + local toolname = iif(cfg.prefix, toolset.gettoolname(cfg, tool), toolset.tools[tool]) + args = table.join(args, toolname) + args = table.join(args, toolset.getcppflags(cfg)) -- Preprocessor + args = table.join(args, toolset.getdefines(cfg.defines)) + args = table.join(args, toolset.getundefines(cfg.undefines)) + args = table.join(args, toolset.getincludedirs(cfg, cfg.includedirs, cfg.sysincludedirs)) + if project.iscpp(prj) then + args = table.join(args, toolset.getcxxflags(cfg)) + else + args = table.join(args, toolset.getcflags(cfg)) + end + args = table.join(args, cfg.buildoptions) + return args + end + + -- Copied from gmake2 module + -- Return default toolset of given config or system default toolset + function m.getToolSet(cfg) + local default = iif(cfg.system == p.MACOSX, "clang", "gcc") + local toolset = p.tools[_OPTIONS.cc or cfg.toolset or default] + if not toolset then + error("Invalid toolset '" .. cfg.toolset .. "'") + end + return toolset + end + + -- Copied from gmake2 module + function m.defaultconfig(target) + -- find the right configuration iterator function for this object + local eachconfig = iif(target.project, project.eachconfig, p.workspace.eachconfig) + local defaultconfig = nil + + -- find the right default configuration platform, grab first configuration that matches + if target.defaultplatform then + for cfg in eachconfig(target) do + if cfg.platform == target.defaultplatform then + defaultconfig = cfg + break + end + end + end + + -- grab the first configuration and write the block + if not defaultconfig then + local iter = eachconfig(target) + defaultconfig = iter() + end + + return defaultconfig + end diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6aa1cbc --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/_bin/Debug/Template", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..2227238 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,27 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "build debug", + "type": "shell", + "command": "premake5 ecc && premake5 gmake2 && make", + "problemMatcher": [], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "build release", + "type": "shell", + "command": "premake5 ecc && premake5 gmake2 && make config=release", + "problemMatcher": [], + "group": { + "kind": "build", + "isDefault": false + } + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 394a9d5..e907f23 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@ -# CPP-SDL-Template - +# CPP-SDL-Template + A template for new C++ SDL projects in vsCode -vsCode Extensions: -clangd -CodeLLDB +vsCode Extensions: +clangd +CodeLLDB -premake5 is used to create the Makefile +premake5 is used to create the Makefile .ecc is used by premake5 to create compile_commands.json for clangd -usage: -in premake5.lua change "workspace" and "project" variable +usage: +in premake5.lua change "workspace" and "project" variable in .vscode/launch.json change "program" to the same name as used in the premake5.lua file above use the command "premake5 vs2022" to create VS files. (not tested) \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..47276e7 --- /dev/null +++ b/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + std::cout << "Hello World!" << std::endl; +} \ No newline at end of file diff --git a/premake5.lua b/premake5.lua new file mode 100644 index 0000000..862bd90 --- /dev/null +++ b/premake5.lua @@ -0,0 +1,27 @@ +workspace "Template" + configurations { "Debug", "Release" } +--ecc scripts from https://github.com/MattBystrin/premake-ecc +require ".ecc/ecc" + +project "Template" + kind "WindowedApp" + language "C++" + targetdir "_bin/%{cfg.buildcfg}" + objdir "_obj/%{cfg.buildcfg}" + + files { "**.h", "**.cpp" } + + filter "configurations:Debug" + defines { "DEBUG" } + symbols "On" + links { "SDL2", "SDL2_image", "SDL2_ttf" } + --links { "SDL2", "SDL2_image", "GL", "GLEW", "SOIL", "fmod", "fmodstudio", "SDL2_ttf" } + --libdirs { "../External/FMOD/lib/x86_64" } + --includedirs { "../External/FMOD/inc" } + + filter "configurations:Release" + defines { "NDEBUG" } + optimize "On" + links { "SDL2", "SDL2_image", "SDL2_ttf" } + --postbuildcommands { "{COPY} %[Assets] %[%{!cfg.targetdir}]" } + \ No newline at end of file