Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate and use a config.h header #1088

Merged
merged 6 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ jobs:
YARP_DEBUG_MODE_BUILD: "1"

build-without-assertions:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
os: [ ubuntu-20.04, ubuntu-22.04 ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
/spec/reports/
/top-100-gems/
/tmp/
/vendor/bundle

/build/
/lib/yarp/yarp.*
/lib/yarp.bundle
/lib/yarp.so
test.rb
*.dSYM
*~

test.c
a.out
Expand All @@ -41,5 +43,7 @@ configure
config.log
config.status
Makefile
/include/yarp/config.h
/config.h.in

tags
23 changes: 5 additions & 18 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ SOEXT := $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]')

DEFS := @DEFS@
CPPFLAGS := @DEFS@ -Iinclude
CFLAGS := @CFLAGS@ -std=c99 -Wall -Werror -Wextra -Wpedantic -Wsign-conversion -fPIC -fvisibility=hidden
CFLAGS := @CFLAGS@ -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wsign-conversion -fPIC -fvisibility=hidden
CC := @CC@

HEADERS := $(shell find include -name '*.h') include/yarp/ast.h
HEADERS := $(shell find include -name '*.h')
SOURCES := $(shell find src -name '*.c')
SHARED_OBJECTS := $(subst src/,build/shared/,$(SOURCES:.c=.o))
STATIC_OBJECTS := $(subst src/,build/static/,$(SOURCES:.c=.o))
Expand All @@ -25,8 +25,6 @@ all: shared static
shared: build/librubyparser.$(SOEXT)
static: build/librubyparser.a

$(OBJECTS): Makefile $(HEADERS)

build/librubyparser.$(SOEXT): $(SHARED_OBJECTS)
$(ECHO) "linking $@"
$(Q) $(CC) $(DEBUG_FLAGS) $(CFLAGS) -shared -o $@ $(SHARED_OBJECTS)
Expand All @@ -35,12 +33,12 @@ build/librubyparser.a: $(STATIC_OBJECTS)
$(ECHO) "building $@"
$(Q) $(AR) $(ARFLAGS) $@ $(STATIC_OBJECTS) $(Q1:0=>/dev/null)

build/shared/%.o: src/%.c
build/shared/%.o: src/%.c Makefile $(HEADERS)
$(ECHO) "compiling $@"
$(Q) mkdir -p $(@D)
$(Q) $(CC) $(DEBUG_FLAGS) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

build/static/%.o: src/%.c
build/static/%.o: src/%.c Makefile $(HEADERS)
$(ECHO) "compiling $@"
$(Q) mkdir -p $(@D)
$(Q) $(CC) $(DEBUG_FLAGS) -DYP_STATIC $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
Expand Down Expand Up @@ -71,19 +69,8 @@ fuzz-run-%: FORCE fuzz-docker-build
$(Q) docker run -it --rm -v $(shell pwd):/yarp -v $(FUZZ_OUTPUT_DIR):/fuzz_output yarp/fuzz /bin/bash -c "./fuzz/$*.sh /fuzz_output/$*"
FORCE:

include/yarp/ast.h: templates/include/yarp/ast.h.erb
$(ECHO) "generating $@"
$(Q) rake $@

clean:
$(Q) rm -f -r \
build \
ext/yarp/node.c \
include/{ast.h,node.h} \
java/org/yarp/{AbstractNodeVisitor.java,Loader.java,Nodes.java} \
lib/yarp/{node,serialize}.rb \
src/{node.c,prettyprint.c,serialize.c,token_type.c} \
$(OBJECTS)
$(Q) rm -f -r build

.PHONY: clean

Expand Down
9 changes: 5 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ def run_script(command)
sh command
end

file "configure" do
file "configure" => ["configure.ac"] do
# autoreconf would be more generic, but it does not seem to work on GitHub Actions on macOS
run_script "autoconf"
run_script "autoheader"
end

file "Makefile" => "configure" do
file "Makefile" => ["configure", "Makefile.in"] do
run_script "./configure"
end

Expand All @@ -62,8 +64,7 @@ end

# So `rake clobber` will delete generated files
CLOBBER.concat(TEMPLATES)
CLOBBER.concat(["configure", "Makefile", "build"])
CLOBBER << Rake::FileList.new("src/**/*.o")
CLOBBER.concat(["configure", "Makefile", "build", "config.h.in", "include/yarp/config.h"])
CLOBBER << "lib/yarp.#{RbConfig::CONFIG["DLEXT"]}"

TEMPLATES.each do |filepath|
Expand Down
23 changes: 18 additions & 5 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
AC_INIT
m4_define([_YP_VERSION_MAJOR], [0])
m4_define([_YP_VERSION_MINOR], [4])
m4_define([_YP_VERSION_PATCH], [0])
m4_define([_YP_VERSION], [_YP_VERSION_MAJOR._YP_VERSION_MINOR._YP_VERSION_PATCH])

AC_INIT([YARP],[_YP_VERSION],[https://github.com/ruby/yarp/issues/new],[yarp],[https://github.com/ruby/yarp])

AC_PROG_CC
AC_DEFINE(_XOPEN_SOURCE, 700)
AC_CHECK_FUNCS(mmap)
AC_CHECK_FUNCS(snprintf)
AC_CHECK_FUNCS(strncasecmp)
AC_DEFINE([_XOPEN_SOURCE], [700], [_XOPEN_SOURCE])
AC_DEFINE([YP_VERSION_MAJOR], [_YP_VERSION_MAJOR], [YP_VERSION_MAJOR])
AC_DEFINE([YP_VERSION_MINOR], [_YP_VERSION_MINOR], [YP_VERSION_MINOR])
AC_DEFINE([YP_VERSION_PATCH], [_YP_VERSION_PATCH], [YP_VERSION_PATCH])
AC_DEFINE([YP_VERSION], ["_YP_VERSION"], [YP_VERSION])

AC_CHECK_FUNCS([mmap])
AC_CHECK_FUNCS([snprintf])
AC_CHECK_FUNCS([strncasecmp])

AC_CONFIG_HEADERS([include/yarp/config.h:config.h.in])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
2 changes: 1 addition & 1 deletion ext/yarp/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ Init_yarp(void) {

// Define the version string here so that we can use the constants defined
// in yarp.h.
rb_define_const(rb_cYARP, "VERSION", rb_sprintf("%d.%d.%d", YP_VERSION_MAJOR, YP_VERSION_MINOR, YP_VERSION_PATCH));
rb_define_const(rb_cYARP, "VERSION", rb_str_new2(EXPECTED_YARP_VERSION));

// First, the functions that have to do with lexing and parsing.
rb_define_singleton_method(rb_cYARP, "dump", dump, -1);
Expand Down
6 changes: 1 addition & 5 deletions include/yarp.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@
#include <strings.h>
#endif

#define YP_VERSION_MAJOR 0
#define YP_VERSION_MINOR 4
#define YP_VERSION_PATCH 0

void yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer);

void yp_print_node(yp_parser_t *parser, yp_node_t *node);

// Returns the YARP version and notably the serialization format
// The YARP version and the serialization format.
YP_EXPORTED_FUNCTION const char * yp_version(void);

// Initialize a parser with the given start and end pointers.
Expand Down
4 changes: 4 additions & 0 deletions include/yarp/defines.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef YARP_DEFINES_H
#define YARP_DEFINES_H

// This file should be included first by any *.h or *.c in YARP

#include "yarp/config.h"

#include <ctype.h>
#include <stdarg.h>
#include <stddef.h>
Expand Down
24 changes: 15 additions & 9 deletions src/yarp.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
#include "yarp.h"

#define YP_STRINGIZE0(expr) #expr
#define YP_STRINGIZE(expr) YP_STRINGIZE0(expr)
#define YP_VERSION_MACRO YP_STRINGIZE(YP_VERSION_MAJOR) "." YP_STRINGIZE(YP_VERSION_MINOR) "." YP_STRINGIZE(YP_VERSION_PATCH)

#define YP_TAB_WHITESPACE_SIZE 8
// The YP_VERSION macro is defined by the build system. If it is not
// present then we need to fail the build, since we're explicitly returning it
// from the yp_version function.
#ifndef YP_VERSION
#error "YP_VERSION must be defined"
#endif

// The YARP version and the serialization format.
const char *
yp_version(void) {
return YP_VERSION_MACRO;
return YP_VERSION;
}

// In heredocs, tabs automatically complete up to the next 8 spaces. This is
// defined in CRuby as TAB_WIDTH.
#define YP_TAB_WHITESPACE_SIZE 8

// Debugging logging will provide you will additional debugging functions as
// well as automatically replace some functions with their debugging
// counterparts.
#ifndef YP_DEBUG_LOGGING
#define YP_DEBUG_LOGGING 0
#endif
Expand Down Expand Up @@ -12885,6 +12894,3 @@ yp_parse_serialize(const char *source, size_t size, yp_buffer_t *buffer) {
#undef YP_CASE_KEYWORD
#undef YP_CASE_OPERATOR
#undef YP_CASE_WRITABLE
#undef YP_STRINGIZE
#undef YP_STRINGIZE0
#undef YP_VERSION_MACRO