added template files

This commit is contained in:
Sarah Faey 2024-09-18 22:53:52 +02:00
parent abaf74850e
commit 2fa0eb2b05
10 changed files with 320 additions and 8 deletions

29
.ecc/LICENSE.md Normal file
View file

@ -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.

49
.ecc/README.md Normal file
View file

@ -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)

4
.ecc/_manifest.lua Normal file
View file

@ -0,0 +1,4 @@
return {
"_preload.lua",
"ecc.lua"
}

35
.ecc/_preload.lua Normal file
View file

@ -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

119
.ecc/ecc.lua Normal file
View file

@ -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

16
.vscode/launch.json vendored Normal file
View file

@ -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}"
}
]
}

27
.vscode/tasks.json vendored Normal file
View file

@ -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
}
}
]
}

6
main.cpp Normal file
View file

@ -0,0 +1,6 @@
#include<iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
}

27
premake5.lua Normal file
View file

@ -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}]" }