Discussion:
[Linuxptp-devel] [PATCH RFC 0/1] Fixing up the compile time feature checks
Richard Cochran
2013-09-29 13:33:06 UTC
Permalink
As is stands now, at least the HAVE_CLOCK_ADJTIME check is broken when
cross compiling, and, as discussed on the list not long ago, the other
"feature" checks are also brittle.

This patch shows how I propose to fix this. Comments and testing are
most welcome, especially from the disto people.

Thanks,
Richard


Richard Cochran (1):
Make the CFLAGS more robust.

incdefs.sh | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
makefile | 14 +++---------
2 files changed, 74 insertions(+), 11 deletions(-)
create mode 100755 incdefs.sh
--
1.7.10.4
Richard Cochran
2013-09-29 13:33:07 UTC
Permalink
This patch introduces a shell script to figure out the proper definitions
for the HAVE_CLOCK_ADJTIME and HAVE_ONESTEP_SYNC macros, as well as the
include path. The intent is to "do the right" for three different user
scenarios, namely cross compiling, compiling against a custom kernel, and
using a plain old disto kernel.

Signed-off-by: Richard Cochran <***@gmail.com>
---
incdefs.sh | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
makefile | 14 +++---------
2 files changed, 74 insertions(+), 11 deletions(-)
create mode 100755 incdefs.sh

diff --git a/incdefs.sh b/incdefs.sh
new file mode 100755
index 0000000..cf00eaf
--- /dev/null
+++ b/incdefs.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+#
+# Discover the CFLAGS to use during compilation.
+#
+# Copyright (C) 2013 Richard Cochran <***@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+#
+# Look for the clock_adjtime functional prototype in the C library.
+#
+user_flags()
+{
+ dirs=$(echo "" | ${CROSS_COMPILE}cpp -Wp,-v 2>&1 >/dev/null | grep ^" /")
+ for d in $dirs; do
+ files=$(find $d -type f -name time.h)
+ for f in $files; do
+ if grep -q clock_adjtime $f; then
+ printf " -D_GNU_SOURCE -DHAVE_CLOCK_ADJTIME"
+ return
+ fi
+ done
+ done
+}
+
+#
+# Find the most appropriate kernel header for the SIOCSHWTSTAMP ioctl.
+#
+# 1. custom kernel or cross build using KBUILD_OUTPUT
+# 2. sanitized headers installed under /lib/modules/`uname -r`/build
+# 3. normal build using standard system headers
+#
+kernel_flags()
+{
+ prefix=""
+ tstamp=/usr/include/linux/net_tstamp.h
+
+ if [ "x$KBUILD_OUTPUT" != "x" ]; then
+ # With KBUILD_OUTPUT set, we are building against
+ # either a custom kernel or a cross compiled kernel.
+ build=${KBUILD_OUTPUT}
+ else
+ # If the currently running kernel is a custom build
+ # with the headers installed, then we should use them.
+ build=/lib/modules/`uname -r`/build
+ fi
+
+ if [ -f ${build}${tstamp} ]; then
+ prefix=${build}
+ printf " -I%s/usr/include" $prefix
+ fi
+
+ if grep -q HWTSTAMP_TX_ONESTEP_SYNC ${prefix}${tstamp}; then
+ printf " -DHAVE_ONESTEP_SYNC"
+ fi
+}
+
+flags="$(user_flags)$(kernel_flags)"
+echo "$flags"
diff --git a/makefile b/makefile
index d79a1df..2eaeca5 100644
--- a/makefile
+++ b/makefile
@@ -15,21 +15,12 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

-KBUILD_OUTPUT ?= /lib/modules/$(shell uname -r)/build
-
-FEAT_CFLAGS :=
-ifneq ($(shell grep --no-messages clock_adjtime /usr/include/bits/time.h),)
-FEAT_CFLAGS += -D_GNU_SOURCE -DHAVE_CLOCK_ADJTIME
-endif
-ifneq ($(shell grep --no-messages HWTSTAMP_TX_ONESTEP_SYNC $(KBUILD_OUTPUT)/usr/include/linux/net_tstamp.h),)
-FEAT_CFLAGS += -DHAVE_ONESTEP_SYNC
-endif
+KBUILD_OUTPUT =

DEBUG =
CC = $(CROSS_COMPILE)gcc
-INC = -I$(KBUILD_OUTPUT)/usr/include
VER = -DVER=$(version)
-CFLAGS = -Wall $(VER) $(INC) $(DEBUG) $(FEAT_CFLAGS) $(EXTRA_CFLAGS)
+CFLAGS = -Wall $(VER) $(incdefs) $(DEBUG) $(EXTRA_CFLAGS)
LDLIBS = -lm -lrt $(EXTRA_LDFLAGS)
PRG = ptp4l pmc phc2sys hwstamp_ctl
OBJ = bmc.o clock.o clockadj.o config.o fault.o fsm.o ptp4l.o mave.o \
@@ -40,6 +31,7 @@ OBJECTS = $(OBJ) hwstamp_ctl.o phc2sys.o pmc.o pmc_common.o sysoff.o
SRC = $(OBJECTS:.o=.c)
DEPEND = $(OBJECTS:.o=.d)
srcdir := $(dir $(lastword $(MAKEFILE_LIST)))
+incdefs := $(shell $(srcdir)/incdefs.sh)
version := $(shell $(srcdir)/version.sh $(srcdir))
VPATH = $(srcdir)
--
1.7.10.4
Keller, Jacob E
2013-09-30 19:53:19 UTC
Permalink
This seems a lot better than what we were doing. I will try this out later today to see how it works.

Regards,
Jake
-----Original Message-----
Sent: Sunday, September 29, 2013 6:33 AM
Subject: [Linuxptp-devel] [PATCH RFC 1/1] Make the CFLAGS more
robust.
This patch introduces a shell script to figure out the proper definitions
for the HAVE_CLOCK_ADJTIME and HAVE_ONESTEP_SYNC macros, as well as the
include path. The intent is to "do the right" for three different user
scenarios, namely cross compiling, compiling against a custom kernel, and
using a plain old disto kernel.
---
incdefs.sh | 71
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++
makefile | 14 +++---------
2 files changed, 74 insertions(+), 11 deletions(-)
create mode 100755 incdefs.sh
diff --git a/incdefs.sh b/incdefs.sh
new file mode 100755
index 0000000..cf00eaf
--- /dev/null
+++ b/incdefs.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+#
+# Discover the CFLAGS to use during compilation.
+#
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+#
+# Look for the clock_adjtime functional prototype in the C library.
+#
+user_flags()
+{
+ dirs=$(echo "" | ${CROSS_COMPILE}cpp -Wp,-v 2>&1 >/dev/null
| grep ^" /")
+ for d in $dirs; do
+ files=$(find $d -type f -name time.h)
+ for f in $files; do
+ if grep -q clock_adjtime $f; then
+ printf " -D_GNU_SOURCE -
DHAVE_CLOCK_ADJTIME"
+ return
+ fi
+ done
+ done
+}
+
+#
+# Find the most appropriate kernel header for the SIOCSHWTSTAMP ioctl.
+#
+# 1. custom kernel or cross build using KBUILD_OUTPUT
+# 2. sanitized headers installed under /lib/modules/`uname -r`/build
+# 3. normal build using standard system headers
+#
+kernel_flags()
+{
+ prefix=""
+ tstamp=/usr/include/linux/net_tstamp.h
+
+ if [ "x$KBUILD_OUTPUT" != "x" ]; then
+ # With KBUILD_OUTPUT set, we are building against
+ # either a custom kernel or a cross compiled kernel.
+ build=${KBUILD_OUTPUT}
+ else
+ # If the currently running kernel is a custom build
+ # with the headers installed, then we should use them.
+ build=/lib/modules/`uname -r`/build
+ fi
+
+ if [ -f ${build}${tstamp} ]; then
+ prefix=${build}
+ printf " -I%s/usr/include" $prefix
+ fi
+
+ if grep -q HWTSTAMP_TX_ONESTEP_SYNC ${prefix}${tstamp};
then
+ printf " -DHAVE_ONESTEP_SYNC"
+ fi
+}
+
+flags="$(user_flags)$(kernel_flags)"
+echo "$flags"
diff --git a/makefile b/makefile
index d79a1df..2eaeca5 100644
--- a/makefile
+++ b/makefile
@@ -15,21 +15,12 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-KBUILD_OUTPUT ?= /lib/modules/$(shell uname -r)/build
-
-FEAT_CFLAGS :=
-ifneq ($(shell grep --no-messages clock_adjtime
/usr/include/bits/time.h),)
-FEAT_CFLAGS += -D_GNU_SOURCE -DHAVE_CLOCK_ADJTIME
-endif
-ifneq ($(shell grep --no-messages HWTSTAMP_TX_ONESTEP_SYNC
$(KBUILD_OUTPUT)/usr/include/linux/net_tstamp.h),)
-FEAT_CFLAGS += -DHAVE_ONESTEP_SYNC
-endif
+KBUILD_OUTPUT =
DEBUG =
CC = $(CROSS_COMPILE)gcc
-INC = -I$(KBUILD_OUTPUT)/usr/include
VER = -DVER=$(version)
-CFLAGS = -Wall $(VER) $(INC) $(DEBUG) $(FEAT_CFLAGS)
$(EXTRA_CFLAGS)
+CFLAGS = -Wall $(VER) $(incdefs) $(DEBUG) $(EXTRA_CFLAGS)
LDLIBS = -lm -lrt $(EXTRA_LDFLAGS)
PRG = ptp4l pmc phc2sys hwstamp_ctl
OBJ = bmc.o clock.o clockadj.o config.o fault.o fsm.o ptp4l.o mave.o \
@@ -40,6 +31,7 @@ OBJECTS = $(OBJ) hwstamp_ctl.o phc2sys.o pmc.o pmc_common.o sysoff.o
SRC = $(OBJECTS:.o=.c)
DEPEND = $(OBJECTS:.o=.d)
srcdir := $(dir $(lastword $(MAKEFILE_LIST)))
+incdefs := $(shell $(srcdir)/incdefs.sh)
version := $(shell $(srcdir)/version.sh $(srcdir))
VPATH = $(srcdir)
--
1.7.10.4
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the
most from
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60133471&iu=/4140/ost
g.clktrk
_______________________________________________
Linuxptp-devel mailing list
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel
Loading...