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

Rework include file/path handling so we can install the compiler #265

Merged
merged 1 commit into from
Feb 1, 2017
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
30 changes: 27 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export IFAIL_TESTS
cpplint_FILES = # Files that should be passed to cpplint (and etags)
ir_DEF_FILES = # Files that generate the IR
extension_frontend_SOURCES = # Files added to libfrontend by extensions
p4include_HEADERS = # p4_16 include files
p4_14include_HEADERS = # p4_14 include files

################ Subproject Makefile inclusions

Expand Down Expand Up @@ -85,6 +87,31 @@ BUILT_SOURCES += \
ir/ir-generated.cpp \
ir/gen-tree-macro.h

######## P4 header files to install

# FIXME -- should these be in $(pkgdatadir) or $(prefix)? setup.h.in
# FIXME -- and check-install-headers: below need to be consistent
p4includedir = $(pkgdatadir)/p4include
p4_14includedir = $(pkgdatadir)/p4_14include
p4include_HEADERS += $(srcdir)/p4include/core.p4 $(srcdir)/p4include/v1model.p4

#### For testing, install headers in the build directory

check-install-headers:
@$(MAKE) install-data pkgdatadir=$(builddir)

check: check-install-headers
recheck: check-install-headers

# FIXME -- should be a way of getting configure to do this automatcially? or automake?
setup.h: $(srcdir)/setup.h.in Makefile
@sed -e 's|@pkgdatadir[@]|$(pkgdatadir)|g' \
-e 's|@prefix[@]|$(prefix)|g' \
-e 's|@p4includedir[@]|$(p4includedir)|g' \
-e 's|@p4_14includedir[@]|$(p4_14includedir)|g' \
$< >$@
setup.o setup.lo: setup.h

################

# Front-end library
Expand All @@ -106,9 +133,6 @@ cpplint_FILES += $(noinst_HEADERS)

################ Misc custom targets

install:
echo "Installation not yet supported."

clean-local:
-rm -f $(BUILT_SOURCES) $(CLEANFILES)

Expand Down
2 changes: 2 additions & 0 deletions backends/ebpf/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ noinst_HEADERS += \

cpplint_FILES += $(p4c_ebpf_UNIFIED) $(p4c_ebpf_NONUNIFIED)

p4include_HEADERS += $(srcdir)/%reldir%/p4include/ebpf_model.p4

# Generated by the targets below
-include ebpftests.mk

Expand Down
File renamed without changes.
5 changes: 1 addition & 4 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,5 @@ autoreconf -i
mkdir -p build # recommended folder for build
sourcedir=`pwd`
cd build
# TODO: the "prefix" is needed for finding the p4include folder.
# It should be an absolute path. This may need to change
# when we have a proper installation procedure.
../configure CXXFLAGS="-g -O0" --prefix=$sourcedir $*
../configure CXXFLAGS="-g -O0" $*
echo "### Configured for building in 'build' folder"
4 changes: 1 addition & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ AC_CHECK_LIB([rt], [clock_gettime], [], [])
AC_CHECK_LIB([gmp], [__gmpz_init], [], [AC_MSG_ERROR([GNU MP not found])])
AC_CHECK_LIB([gmpxx], [__gmpz_init], [], [AC_MSG_ERROR([GNU MP not found])])

AC_SUBST(P4INCLUDE, $prefix/p4include)

AC_CONFIG_FILES([Makefile setup.h])
AC_CONFIG_FILES([Makefile])
AX_PYTHON_MODULE([difflib], [fatal], [python])
AX_PYTHON_MODULE([shutil], [fatal], [python])
AX_PYTHON_MODULE([tempfile], [fatal], [python])
Expand Down
42 changes: 40 additions & 2 deletions frontends/common/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ limitations under the License.
*/

#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "setup.h"
#include "options.h"
Expand Down Expand Up @@ -130,6 +133,41 @@ void CompilerOptions::setInputFile() {
}
}

std::vector<const char*>* CompilerOptions::process(int argc, char* const argv[]) {
char buffer[PATH_MAX];
int len;
struct stat st;
/* find the path of the executable. We use a number of techniques that may fail
* or work on different systems, and take the first working one we find. Fall
* back to not overriding the compiled-in installation path */
if ((len = readlink("/proc/self/exe", buffer, sizeof(buffer))) > 0 ||
(len = readlink("/proc/curproc/exe", buffer, sizeof(buffer))) > 0 ||
(len = readlink("/proc/curproc/file", buffer, sizeof(buffer))) > 0 ||
(len = readlink("/proc/self/path/a.out", buffer, sizeof(buffer))) > 0) {
buffer[len] = 0;
} else if (argv[0][0] == '/') {
snprintf(buffer, sizeof(buffer), "%s", argv[0]);
} else if (strchr(argv[0], '/')) {
getcwd(buffer, sizeof(buffer));
strncat(buffer, argv[0], sizeof(buffer) - strlen(buffer) - 1);
} else if (getenv("_")) {
strncpy(buffer, getenv("_"), sizeof(buffer));
buffer[sizeof(buffer) - 1] = 0;
} else {
buffer[0] = 0; }

if (char *p = strrchr(buffer, '/')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that you are assuming that the include path shares the same prefix with the binary path.
Is this a common assumption?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for running tests with a binary in the build directory -- iff there's a p4include directory in the same directory as the executable, it overrides the installed config directory.

++p;
snprintf(p, buffer + sizeof(buffer) - p, "p4include");
if (stat(buffer, &st) >= 0 && S_ISDIR(st.st_mode))
p4includePath = strdup(buffer);
snprintf(p, buffer + sizeof(buffer) - p, "p4_14include");
if (stat(buffer, &st) >= 0 && S_ISDIR(st.st_mode))
p4_14includePath = strdup(buffer); }

return Util::Options::process(argc, argv);
}

FILE* CompilerOptions::preprocess() {
FILE* in = nullptr;

Expand All @@ -145,8 +183,8 @@ FILE* CompilerOptions::preprocess() {
#else
std::string cmd("cpp");
#endif
cmd += cstring(" -undef -nostdinc -I") +
p4includePath + " " + preprocessor_options + " " + file;
cmd += cstring(" -undef -nostdinc") + " " + preprocessor_options
+ " -I" + (isv1() ? p4_14includePath : p4includePath) + " " + file;
if (Log::verbose())
std::cerr << "Invoking preprocessor " << std::endl << cmd << std::endl;
in = popen(cmd.c_str(), "r");
Expand Down
1 change: 1 addition & 0 deletions frontends/common/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CompilerOptions : public Util::Options {

public:
CompilerOptions();
std::vector<const char*>* process(int argc, char* const argv[]);

enum class FrontendVersion {
P4_14,
Expand Down
1 change: 1 addition & 0 deletions setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ limitations under the License.
#include "setup.h"

const char* p4includePath = P4_SYSTEM_INCLUDE_PATH;
const char* p4_14includePath = P4_14_SYSTEM_INCLUDE_PATH;
4 changes: 3 additions & 1 deletion setup.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#ifndef _SETUP_H_
#define _SETUP_H_

#define P4_SYSTEM_INCLUDE_PATH "@P4INCLUDE@"
#define P4_SYSTEM_INCLUDE_PATH "@p4includedir@"
#define P4_14_SYSTEM_INCLUDE_PATH "@p4_14includedir@"
extern const char* p4includePath;
extern const char* p4_14includePath;

#endif /* _SETUP_H_ */