Skip to content

Commit

Permalink
Merge pull request #544
Browse files Browse the repository at this point in the history
To address #516, I'm planning to use Luarocks as a compilation backend. If we set things up the right way, we should be able to use Luarocks to compile our c files into shared libraries, in a cross-platform manner.

This PR has some CFLAGS-related changes to pave the way for that. When using Luarocks to build C modules, the only way to pass CFLAGS is via the CFLAGS environment variable. So what this commit does is take the flags we were passing directly in the c_compiler.lua and passing them via CFLAGS environment variable instead. This implies in a change in behavior. By default we will no longer compile with -g and -Wall. IMO, that might actually be a good thing. The -g causes larger executables and the -Wall is not relevant to end-users. (I'm still leaving the -Wall in the ./run-tests though)
  • Loading branch information
hugomg authored May 26, 2022
2 parents 78d733e + 44ae7e8 commit 1a369b2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 26 deletions.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,15 @@ It is possible to change the compiler optimization level, for the Pallene compil
Here are some examples:

```sh
# execute no optimization (Pallene and C compiler)
# disable Pallene optimization passes
pallenec test.pln -O0

# execute Pallene compiler optimizations and C compiler level 3 optimizations
pallenec test.pln -O3

# execute no optimizations for Pallene compiler but executes C compiler level 2 optimizations
env CFLAGS="-O2" pallenec test.pln -O0

# execute all default optimizations (same as -O2)
# disable C compiler optimization
export CFLAGS='-O0'
pallenec test.pln
```

**Note**: For the C compiler only, the setting set using `CFLAGS` overrides the setting set by flag `-O`.

For more compiler options, see `pallenec --help`
For more compiler options, see `./pallenec --help`

## Developing Pallene

Expand Down
6 changes: 3 additions & 3 deletions run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ echo "--- Test Suite ---"
# need to press Ctrl-C once and busted usually gets to exit gracefully.
FLAGS=(--verbose --no-keep-going)

# To speed things up, we tell the C compiler to skip optimizations.
# Don't worry, the continuous integration server still tests with optimizations.
export CFLAGS=-O0
# To speed things up, we tell the C compiler to skip optimizations. (It's OK, the CI still uses -O2)
# Also, add some compiler flags to verify standard compliance.
export CFLAGS='-O0 -std=c99 -Wall -Werror -Wundef -Wpedantic -Wno-unused'

if [ "$#" -eq 0 ]; then
if command -v parallel >/dev/null; then
Expand Down
17 changes: 5 additions & 12 deletions src/pallene/c_compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ local util = require "pallene.util"

local c_compiler = {}

local CC = "cc"
local CPPFLAGS = ""
local CFLAGS_BASE = "-std=c99 -g -fPIC"
local CFLAGS_WARN = "-Wall -Wundef -Wpedantic -Wno-unused"
local USER_CFlAGS = os.getenv("CFLAGS") or ""
local CC = os.getenv("CC") or "cc"
local CFLAGS = os.getenv("CFLAGS") or "-O2"

local function get_uname()
local ok, err, uname = util.outputs_of_execute("uname -s")
Expand All @@ -47,14 +44,10 @@ local function run_cc(args)
return true, {}
end

-- The third argument is the mod_name, which is not used by this
function c_compiler.compile_c_to_o(in_filename, out_filename, _, opt_level)
function c_compiler.compile_c_to_o(in_filename, out_filename)
return run_cc({
CPPFLAGS,
CFLAGS_BASE,
CFLAGS_WARN,
opt_level and "-O"..opt_level or "",
USER_CFlAGS,
"-fPIC",
CFLAGS,
"-x c",
"-o", util.shell_quote(out_filename),
"-c", util.shell_quote(in_filename),
Expand Down

0 comments on commit 1a369b2

Please sign in to comment.