Skip to contentSkip to conversation

kbuild: use target compiler for user binaries in samples/

PATCH In review Sam Ravnborg 5 participants 20 messages Jul 13, 2014 – Aug 21, 2014

Subsystems (5)
Documentation x86 Kbuild s390 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover kbuild: use target compiler for user binaries in samples/

[PATCH v2 0/6] kbuild: use target compiler for user binaries in samples/

The user-space binaries build in samples/ uses the installed headers for the kernel.
Therefore it is wrong that they use the host compiler to build the samples.

This set of patches add kbuild support for building the userspace programs.
They are build with same endian and bit-size as the kernel, so in other words no support for buildign 32 bit userspace for a 64 bit kernel.
The majority of the patch is the Documentation changes, especially because a new chapter was added.

NOTE: This will likely break the build if the toolchain used do not include a libc. Let's see how much noise this creates.

I have included a few moe people in this submission as I assume this has their interest.

Michal - I hope you can take this via the kbuild#misc tree when any feedback has been addressed.

Changes since first posting (https://lkml.org/lkml/2013/3/31/74):
- Use $(biarch) in X86_32 (feedback from Michael Marek)
- Drop sub-directory support (feedback from Michael Marek)
o This simplified the uapi support a lot
- Fix clean support (feedback from Michael Marek)
- Added documentation to Documentation/kbuild/
- Use $(CC) as there is no need for a dedicated UAPICC
- Prefer foobar-y over foobar-objs for specifying .o files for a binary
- Move cc options to Makefile.uapiprogs and only use -Wall
- Use -isystem to include installed uapi headers before headers provided by the installed libc
- Divided up in smaller patches to ease review

Sam

Sam Ravnborg (6):
kbuild: add support for building userspace api programs
samples: refactor Makefile
samples: use uapiprogs support for seccomp
samples: use uapiprogs support for hidraw
samples: fix warnings in uhid-example
samples: use uapiprogs support for uhid

 Documentation/kbuild/kbuild.txt    |   5 ++
 Documentation/kbuild/makefiles.txt | 132 ++++++++++++++++++++++++-------------
 Makefile                           |   4 ++
 arch/s390/Makefile                 |   2 +
 arch/sparc/Makefile                |   2 +
 arch/x86/Makefile                  |   2 +
 samples/Makefile                   |  16 ++++-
 samples/hidraw/Makefile            |  11 +---
 samples/seccomp/Makefile           |  48 +-------------
 samples/uhid/Makefile              |  11 +---
 samples/uhid/uhid-example.c        |   9 ++-
 scripts/Makefile.build             |   4 ++
 scripts/Makefile.clean             |   9 +--
 scripts/Makefile.uapiprogs         |  63 ++++++++++++++++++
 14 files changed, 196 insertions(+), 122 deletions(-)
#1 lore raw

PATCH v2 1/6 kbuild: add support for building userspace api programs

Add dedicated support for building the usersapce api sample programs that is located in samples/
The dedicated kbuild support allows us to use much simpler Makefile fragments while building the userspace programs for the same host as the kernel.

The syntax looks like this:

uapiprogs-y := hid-example

This will build the userspace program hid-example from hid-example.c and include files will be preferred from the exported uapi headers.

The userspace sample programs rely on the exported uapi header files and thus are supposed to be built for the same architecture as the kernel. Bit bit-size will be the same as used for the kernel so there is no support for building 32 bit binaries for a 64 bit kernel.

To build the userspace programs the toolchain needs to include support for this - so this change will break the build for the setups that rely on toolchain without any libc included.

One fix for this would be to add a libc to the kernel, but this seems to be a rather drastic measure just to support building a few sample programs.
The better fix is to use toolchains with libc support.

This patch introduces only the infrastructure - including the setup for various architectures to specify -m option.

Cc Thierry Reding <[email protected]>
Cc Stephen Rothwell <[email protected]>
---
 Documentation/kbuild/kbuild.txt    |   5 ++
 Documentation/kbuild/makefiles.txt | 132 ++++++++++++++++++++++++-------------
 Makefile                           |   4 ++
 arch/s390/Makefile                 |   2 +
 arch/sparc/Makefile                |   2 +
 arch/x86/Makefile                  |   2 +
 scripts/Makefile.build             |   4 ++
 scripts/Makefile.clean             |   9 +--
 scripts/Makefile.uapiprogs         |  63 ++++++++++++++++++
 9 files changed, 172 insertions(+), 51 deletions(-)
 create mode 100644 scripts/Makefile.uapiprogs
Documentation/kbuild/kbuild.txt +5 -0
index 6466704..6b691d3b 100644
@@ -233,3 +233,8 @@ KBUILD_VMLINUX_MAIN
 All object files for the main part of vmlinux.
 KBUILD_VMLINUX_INIT and KBUILD_VMLINUX_MAIN together specify
 all the object files used to link vmlinux.
+
+UAPICFLAGS
+--------------------------------------------------
+Arch specific flags used when building sample binaries.
+This is usually the machine size (32 versus 64 bit).
Documentation/kbuild/makefiles.txt +85 -47

270 lines not on this page · raw message

Makefile +4 -0
index 4d75b4b..7fda2ba 100644
@@ -935,8 +935,12 @@ prepare1: prepare2 $(version_h) include/generated/utsrelease.h \
 
 archprepare: archheaders archscripts prepare1 scripts_basic
 
+# To build userspace samples the uapi headers needs to be exported
 prepare0: archprepare FORCE
 	$(Q)$(MAKE) $(build)=.
+ifdef CONFIG_SAMPLES
+	$(Q)$(MAKE) KBUILD_SRC= headers_install
+endif
 
 # All the preparing..
 prepare: prepare0
arch/s390/Makefile +2 -0
index 874e6d6..4b32933 100644
@@ -18,6 +18,7 @@ LD_BFD		:= elf32-s390
 LDFLAGS		:= -m elf_s390
 KBUILD_CFLAGS	+= -m31
 KBUILD_AFLAGS	+= -m31
+UAPICFLAGS	+= -m31
 UTS_MACHINE	:= s390
 STACK_SIZE	:= 8192
 CHECKFLAGS	+= -D__s390__ -msize-long
@@ -28,6 +29,7 @@ KBUILD_AFLAGS_MODULE += -fPIC
 KBUILD_CFLAGS_MODULE += -fPIC
 KBUILD_CFLAGS	+= -m64
 KBUILD_AFLAGS	+= -m64
+UAPICFLAGS	+= -m64
 UTS_MACHINE	:= s390x
 STACK_SIZE	:= 16384
 CHECKFLAGS	+= -D__s390__ -D__s390x__
arch/sparc/Makefile +2 -0
index 9ff4236..08a5f78 100644
@@ -26,6 +26,7 @@ UTS_MACHINE    := sparc
 
 KBUILD_CFLAGS  += -m32 -mcpu=v8 -pipe -mno-fpu -fcall-used-g5 -fcall-used-g7
 KBUILD_AFLAGS  += -m32 -Wa,-Av8
+UAPICFLAGS     += -m32
 
 else
 #####
@@ -42,6 +43,7 @@ KBUILD_CFLAGS += -ffixed-g4 -ffixed-g5 -fcall-used-g7 -Wno-sign-compare
 KBUILD_CFLAGS += -Wa,--undeclared-regs
 KBUILD_CFLAGS += $(call cc-option,-mtune=ultrasparc3)
 KBUILD_AFLAGS += -m64 -mcpu=ultrasparc -Wa,--undeclared-regs
+UAPICFLAGS    += -m64
 
 ifeq ($(CONFIG_MCOUNT),y)
   KBUILD_CFLAGS += -pg
arch/x86/Makefile +2 -0
index 33f71b0..4f21420 100644
@@ -50,6 +50,7 @@ ifeq ($(CONFIG_X86_32),y)
         biarch := $(call cc-option,-m32)
         KBUILD_AFLAGS += $(biarch)
         KBUILD_CFLAGS += $(biarch)
+        UAPICFLAGS    += $(biarch)
 
         KBUILD_CFLAGS += -msoft-float -mregparm=3 -freg-struct-return
 
@@ -82,6 +83,7 @@ else
         biarch := -m64
         KBUILD_AFLAGS += -m64
         KBUILD_CFLAGS += -m64
+        UAPICFLAGS    += -m64
 
         # Don't autogenerate traditional x87, MMX or SSE instructions
         KBUILD_CFLAGS += -mno-mmx -mno-sse
scripts/Makefile.build +4 -0
index bf3e677..5526769 100644
@@ -64,6 +64,10 @@ ifneq ($(hostprogs-y)$(hostprogs-m),)
 include scripts/Makefile.host
 endif
 
+ifdef uapiprogs-y
+include scripts/Makefile.uapiprogs
+endif
+
 ifneq ($(KBUILD_SRC),)
 # Create output directory if not already present
 _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
scripts/Makefile.clean +5 -4
index 686cb0d..aee43fa 100644
@@ -40,10 +40,11 @@ subdir-ymn	:= $(addprefix $(obj)/,$(subdir-ymn))
 # build a list of files to remove, usually relative to the current
 # directory
 
-__clean-files	:= $(extra-y) $(always)                  \
-		   $(targets) $(clean-files)             \
-		   $(host-progs)                         \
-		   $(hostprogs-y) $(hostprogs-m) $(hostprogs-)
+__clean-files	:= $(extra-y) $(always)                        \
+		   $(targets) $(clean-files)                   \
+		   $(host-progs)                               \
+		   $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \
+		   $(uapiprogs-y) $(uapiprogs-)
 
 __clean-files   := $(filter-out $(no-clean-files), $(__clean-files))
 
scripts/Makefile.uapiprogs new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
1.9.3
#2 lore raw

PATCH v2 2/6 samples: refactor Makefile

Use one line per module/program in Makefile.
This style is easier to read/extend.

Introduce inverse xmas style sorting.
This may prevent some merge conflicts in the future

---
 samples/Makefile | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
samples/Makefile +13 -2
index 1a60c62..7c08028 100644
@@ -1,4 +1,15 @@
 # Makefile for Linux samples code
+# Sort entries using inverse xmas style
+#
+# kernel modules
+obj-$(CONFIG_SAMPLES) += hw_breakpoint/
+obj-$(CONFIG_SAMPLES) += trace_events/
+obj-$(CONFIG_SAMPLES) += kprobes/
+obj-$(CONFIG_SAMPLES) += kobject/
+obj-$(CONFIG_SAMPLES) += kfifo/
+obj-$(CONFIG_SAMPLES) += rpmsg/
+obj-$(CONFIG_SAMPLES) += kdb/
 
-obj-$(CONFIG_SAMPLES)	+= kobject/ kprobes/ trace_events/ \
-			   hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ seccomp/
+# user space programs
+obj-$(CONFIG_SAMPLES) += seccomp/
+obj-$(CONFIG_SAMPLES) += hidraw/
Signature · 1 line
1.9.3
reviewer
#3 lore raw

Re: [PATCH 2/6] samples: refactor Makefile

Quoted 4 lines · Sam Ravnborg

Use one line per module/program in Makefile.
This style is easier to read/extend.

Introduce inverse xmas style sorting.

google search couldn't tell me what that means and I cannot deduce it from the new Makefile ordering (if there is some ordering). Please explain.

Thanks.

Quoted 30 lines

This may prevent some merge conflicts in the future

---
 samples/Makefile | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
samples/Makefile +13 -2
index 1a60c62..7c08028 100644
@@ -1,4 +1,15 @@
 # Makefile for Linux samples code
+# Sort entries using inverse xmas style
+#
+# kernel modules
+obj-$(CONFIG_SAMPLES) += hw_breakpoint/
+obj-$(CONFIG_SAMPLES) += trace_events/
+obj-$(CONFIG_SAMPLES) += kprobes/
+obj-$(CONFIG_SAMPLES) += kobject/
+obj-$(CONFIG_SAMPLES) += kfifo/
+obj-$(CONFIG_SAMPLES) += rpmsg/
+obj-$(CONFIG_SAMPLES) += kdb/
 
-obj-$(CONFIG_SAMPLES)	+= kobject/ kprobes/ trace_events/ \
-			   hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ seccomp/
+# user space programs
+obj-$(CONFIG_SAMPLES) += seccomp/
+obj-$(CONFIG_SAMPLES) += hidraw/
Signature · 1 line
~Randy
#4 lore raw

Re: [PATCH 2/6] samples: refactor Makefile

Hi Randy - thanks for the feedback.

Quoted 9 lines · Randy Dunlap
Quoted 4 lines · Sam Ravnborg

Use one line per module/program in Makefile.
This style is easier to read/extend.

Introduce inverse xmas style sorting.

google search couldn't tell me what that means and I cannot deduce it from the new Makefile ordering (if there is some ordering). Please explain.

Sort by length - with longest entry first. Like an xmas tree upside-down. For entries with same lenght sort alphabetically.

Advocated in some places for includes and other places for local variables.

One recent reference is for example this:
http://marc.info/?l=linux-netdev&m=140510662905580&w=2

I will rephrase it like this:

# Sort entries using inverse xmas style:
# Longest entries first, alphabetically when legth is the same.

OK?
Sam

Quoted 37 lines

Thanks.

Quoted 30 lines

This may prevent some merge conflicts in the future

---
 samples/Makefile | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
samples/Makefile +13 -2
index 1a60c62..7c08028 100644
@@ -1,4 +1,15 @@
 # Makefile for Linux samples code
+# Sort entries using inverse xmas style
+#
+# kernel modules
+obj-$(CONFIG_SAMPLES) += hw_breakpoint/
+obj-$(CONFIG_SAMPLES) += trace_events/
+obj-$(CONFIG_SAMPLES) += kprobes/
+obj-$(CONFIG_SAMPLES) += kobject/
+obj-$(CONFIG_SAMPLES) += kfifo/
+obj-$(CONFIG_SAMPLES) += rpmsg/
+obj-$(CONFIG_SAMPLES) += kdb/
 
-obj-$(CONFIG_SAMPLES)	+= kobject/ kprobes/ trace_events/ \
-			   hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ seccomp/
+# user space programs
+obj-$(CONFIG_SAMPLES) += seccomp/
+obj-$(CONFIG_SAMPLES) += hidraw/
Signature · 1 line
~Randy
reviewer
#5 lore raw

Re: [PATCH 2/6] samples: refactor Makefile

Quoted 18 lines · Sam Ravnborg

Hi Randy - thanks for the feedback.

Quoted 9 lines · Randy Dunlap
Quoted 4 lines · Sam Ravnborg

Use one line per module/program in Makefile.
This style is easier to read/extend.

Introduce inverse xmas style sorting.

google search couldn't tell me what that means and I cannot deduce it from the new Makefile ordering (if there is some ordering). Please explain.

Sort by length - with longest entry first. Like an xmas tree upside-down. For entries with same lenght sort alphabetically.

Advocated in some places for includes and other places for local variables.

Quoted 7 lines

One recent reference is for example this:
http://marc.info/?l=linux-netdev&m=140510662905580&w=2

I will rephrase it like this:

# Sort entries using inverse xmas style:
# Longest entries first, alphabetically when legth is the same.

length

Quoted 2 lines

OK?

Yes. Thanks.

Quoted 36 lines

Sam

Quoted 32 lines

Thanks.

Quoted 29 lines

This may prevent some merge conflicts in the future

---
 samples/Makefile | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
samples/Makefile +13 -2
index 1a60c62..7c08028 100644
@@ -1,4 +1,15 @@
 # Makefile for Linux samples code
+# Sort entries using inverse xmas style
+#
+# kernel modules
+obj-$(CONFIG_SAMPLES) += hw_breakpoint/
+obj-$(CONFIG_SAMPLES) += trace_events/
+obj-$(CONFIG_SAMPLES) += kprobes/
+obj-$(CONFIG_SAMPLES) += kobject/
+obj-$(CONFIG_SAMPLES) += kfifo/
+obj-$(CONFIG_SAMPLES) += rpmsg/
+obj-$(CONFIG_SAMPLES) += kdb/
 
-obj-$(CONFIG_SAMPLES)	+= kobject/ kprobes/ trace_events/ \
-			   hw_breakpoint/ kfifo/ kdb/ hidraw/ rpmsg/ seccomp/
+# user space programs
+obj-$(CONFIG_SAMPLES) += seccomp/
+obj-$(CONFIG_SAMPLES) += hidraw/
Signature · 1 line
~Randy
#6 lore raw

Re: [PATCH 2/6] samples: refactor Makefile

In reply to Sam Ravnborg

Hi Sam,

Quoted 6 lines · On Sun, 13 Jul 2014 21:52:36 +0200 Sam Ravnborg <[email protected]> wrote:
Quoted 4 lines · Randy Dunlap
Quoted 3 lines · Sam Ravnborg

Use one line per module/program in Makefile.
This style is easier to read/extend.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Introduce inverse xmas style sorting.

google search couldn't tell me what that means and I cannot deduce it from the new Makefile ordering (if there is some ordering). Please explain.

Sort by length - with longest entry first. Like an xmas tree upside-down. For entries with same lenght sort alphabetically.

If you are going to sort, just sort alphabetically ... this length based sorting just means you have to search to find anything ... It looks pretty, but that is about all.

Signature · 2 lines
Cheers,
Stephen Rothwell                    [email protected]
#7 lore raw

PATCH v2 3/6 samples: use uapiprogs support for seccomp

In reply to Sam Ravnborg

Reduce complexity of seccomp Makefile by introducing use of uapiprogs support. Build testet with i686 only.

---
 samples/Makefile         |  2 +-
 samples/seccomp/Makefile | 48 ++----------------------------------------------
 2 files changed, 3 insertions(+), 47 deletions(-)
samples/Makefile +1 -1
index 7c08028..b4d7d66 100644
@@ -11,5 +11,5 @@ obj-$(CONFIG_SAMPLES) += rpmsg/
 obj-$(CONFIG_SAMPLES) += kdb/
 
 # user space programs
-obj-$(CONFIG_SAMPLES) += seccomp/
+subdir-$(CONFIG_SAMPLES) += seccomp
 obj-$(CONFIG_SAMPLES) += hidraw/
samples/seccomp/Makefile +2 -46

50 lines not on this page · raw message

Signature · 1 line
1.9.3
#8 lore raw

Re: [PATCH 3/6] samples: use uapiprogs support for seccomp

Hi Sam,

Quoted 2 lines · On Sun, 13 Jul 2014 20:42:51 +0200 Sam Ravnborg <[email protected]> wrote:

Reduce complexity of seccomp Makefile by introducing use of uapiprogs support. Build testet with i686 only.

tested ?

(Ditto in 4/6)

Quoted 6 lines
samples/seccomp/Makefile +1 -2
@@ -1,48 +1,4 @@
-# kbuild trick to avoid linker error. Can be omitted if a module is built.
-obj- := dummy.o
+uapiprogs-$(CONFIG_SECCOMP_FILTER) := bpf-fancy dropper bpf-direct

samples/seccomp/Makefile line 11

Quoted 10 lines
samples/seccomp/Makefile +1 -7
 
-hostprogs-$(CONFIG_SECCOMP_FILTER) := bpf-fancy dropper bpf-direct
+bpf-fancy-y := bpf-fancy.o bpf-helper.o
 
-HOSTCFLAGS_bpf-fancy.o += -I$(objtree)/usr/include
-HOSTCFLAGS_bpf-fancy.o += -idirafter $(objtree)/include
-HOSTCFLAGS_bpf-helper.o += -I$(objtree)/usr/include
-HOSTCFLAGS_bpf-helper.o += -idirafter $(objtree)/include
-bpf-fancy-objs := bpf-fancy.o bpf-helper.o
-

I am interested in this series.

Forgive me if I am asking a stupid question..

How can I build hostprogs for the same host as the kernel? (I mean, how can I override $(HOSTCC) for cross-compiling ?)

Best Regards
Masahiro Yamada

#9 lore raw

Re: [PATCH 3/6] samples: use uapiprogs support for seccomp

Quoted 6 lines · Masahiro Yamada

Hi Sam,

On Sun, 13 Jul 2014 20:42:51 +0200
Sam Ravnborg <[email protected]> wrote:

Quoted 6 lines
Quoted 2 lines

Reduce complexity of seccomp Makefile by introducing use of uapiprogs support. Build testet with i686 only.

tested ?

(Ditto in 4/6)

Thanks - will fix.

Quoted 7 lines

I am interested in this series.

Forgive me if I am asking a stupid question..

How can I build hostprogs for the same host as the kernel? (I mean, how can I override $(HOSTCC) for cross-compiling ?)

One way to do this:

make ARCH=sparc CROSS_COMPILE=sparc-leon-linux-gnu- HOSTCC=sparc-leon-linux-gnu-gcc

Notice that I override HOSTCC as part of the make comand line. This allows me to override the "=" assignment in the top-level Makefile.

But then my build breaks as fixdep do not run on my host.

If you in general want to build a binary for the target then uapiprogs come in handy. Only caveat here is that you will be using the exported kernel headers.

Sam

#10 lore raw

Re: [PATCH 3/6] samples: use uapiprogs support for seccomp

Quoted 7 lines · On Wed, 16 Jul 2014 12:31:21 +0200 Sam Ravnborg <[email protected]> wrote:
Quoted 2 lines

How can I build hostprogs for the same host as the kernel? (I mean, how can I override $(HOSTCC) for cross-compiling ?)

One way to do this:

make ARCH=sparc CROSS_COMPILE=sparc-leon-linux-gnu- HOSTCC=sparc-leon-linux-gnu-gcc

Quoted 8 lines

Notice that I override HOSTCC as part of the make comand line. This allows me to override the "=" assignment in the top-level Makefile.

But then my build breaks as fixdep do not run on my host.

If you in general want to build a binary for the target then uapiprogs come in handy. Only caveat here is that you will be using the exported kernel headers.

Thanks for explaining this. It is clearer to me now.

At first I thought this patch was a clean-up,
but it is indeed a big improvement.
Thanks!

Best Regards
Masahiro Yamada

#11 lore raw

PATCH v2 4/6 samples: use uapiprogs support for hidraw

In reply to Sam Ravnborg

Reduce complexity of hidraw Makefile by introducing use of uapiprogs support. Build testet with i386 + sparc32.

Cc David Herrmann <[email protected]>
Cc Jiri Kosina <[email protected]>
---
 samples/Makefile        |  2 +-
 samples/hidraw/Makefile | 11 +----------
 2 files changed, 2 insertions(+), 11 deletions(-)
samples/Makefile +1 -1
index b4d7d66..8fc6c45 100644
@@ -12,4 +12,4 @@ obj-$(CONFIG_SAMPLES) += kdb/
 
 # user space programs
 subdir-$(CONFIG_SAMPLES) += seccomp
-obj-$(CONFIG_SAMPLES) += hidraw/
+subdir-$(CONFIG_SAMPLES) += hidraw
samples/hidraw/Makefile +1 -10
index 382eeae..c2d2544 100644
@@ -1,10 +1 @@
-# kbuild trick to avoid linker error. Can be omitted if a module is built.
-obj- := dummy.o
-
-# List of programs to build
-hostprogs-y := hid-example
-
-# Tell kbuild to always build the programs
-always := $(hostprogs-y)
-
-HOSTCFLAGS_hid-example.o += -I$(objtree)/usr/include
+uapiprogs-y := hid-example
Signature · 1 line
1.9.3
#12 lore raw

PATCH v2 5/6 samples: fix warnings in uhid-example

In reply to Sam Ravnborg

Fix following warnings seen when building using i686:
uhid-example.c: In function ‘uhid_write’:
uhid-example.c:168:4: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘ssize_t’ [-Wformat=] uhid-example.c:168:4: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 4 has type ‘unsigned int’ [-Wformat=] uhid-example.c: In function ‘event’:
uhid-example.c:239:4: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘ssize_t’ [-Wformat=] uhid-example.c:239:4: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 4 has type ‘unsigned int’ [-Wformat=]

Use "%xd" for arguments of type size_t to fix the warnings.

Cc David Herrmann <[email protected]>
Cc Jiri Kosina <[email protected]>
---
 samples/uhid/uhid-example.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
samples/uhid/uhid-example.c +2 -2
index 7d58a4b..f25a5ef 100644
@@ -164,7 +164,7 @@ static int uhid_write(int fd, const struct uhid_event *ev)
 		fprintf(stderr, "Cannot write to uhid: %m\n");
 		return -errno;
 	} else if (ret != sizeof(*ev)) {
-		fprintf(stderr, "Wrong size written to uhid: %ld != %lu\n",
+		fprintf(stderr, "Wrong size written to uhid: %zd != %zd\n",
 			ret, sizeof(ev));
 		return -EFAULT;
 	} else {
@@ -235,7 +235,7 @@ static int event(int fd)
 		fprintf(stderr, "Cannot read uhid-cdev: %m\n");
 		return -errno;
 	} else if (ret != sizeof(ev)) {
-		fprintf(stderr, "Invalid size read from uhid-dev: %ld != %lu\n",
+		fprintf(stderr, "Invalid size read from uhid-dev: %zd != %zd\n",
 			ret, sizeof(ev));
 		return -EFAULT;
 	}
Signature · 1 line
1.9.3
#13 lore raw

PATCH v2 6/6 samples: use uapiprogs support for uhid

In reply to Sam Ravnborg

uhid was not included in the regular builds as it was not wired up in samples/Makefile.

Fix build on sparc32 by defining __USE_GNU to get visibility to O_CLOEXEC.

Cc David Herrmann <[email protected]>
Cc Jiri Kosina <[email protected]>
---
 samples/Makefile            |  1 +
 samples/uhid/Makefile       | 11 +----------
 samples/uhid/uhid-example.c |  5 ++++-
 3 files changed, 6 insertions(+), 11 deletions(-)
samples/Makefile +1 -0
index 8fc6c45..29a09bd 100644
@@ -13,3 +13,4 @@ obj-$(CONFIG_SAMPLES) += kdb/
 # user space programs
 subdir-$(CONFIG_SAMPLES) += seccomp
 subdir-$(CONFIG_SAMPLES) += hidraw
+subdir-$(CONFIG_SAMPLES) += uhid
samples/uhid/Makefile +1 -10
index c95a696..75ff2bb 100644
@@ -1,10 +1 @@
-# kbuild trick to avoid linker error. Can be omitted if a module is built.
-obj- := dummy.o
-
-# List of programs to build
-hostprogs-y := uhid-example
-
-# Tell kbuild to always build the programs
-always := $(hostprogs-y)
-
-HOSTCFLAGS_uhid-example.o += -I$(objtree)/usr/include
+uapiprogs-y := uhid-example
samples/uhid/uhid-example.c +4 -1
index f25a5ef..e4379c8 100644
@@ -37,7 +37,6 @@
  */
 
 #include <errno.h>
-#include <fcntl.h>
 #include <poll.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -45,6 +44,10 @@
 #include <string.h>
 #include <termios.h>
 #include <unistd.h>
+
+#define __USE_GNU
+#include <fcntl.h>
+
 #include <linux/uhid.h>
 
 /*
Signature · 1 line
1.9.3
reviewer
#14 lore raw

Re: [PATCH 1/6] kbuild: add support for building userspace api programs

In reply to Sam Ravnborg

Hi Sam,

Documentation/kbuild/makefiles.txt Both possibilities are described in the following. · line 768

Quoted 13 lines · Sam Ravnborg
Documentation/kbuild/makefiles.txt +3 -1
index c600e2f..713ea10 100644
@@ -761,7 +763,43 @@ Both possibilities are described in the following.
 	like hostprogs-y. But only hostprogs-y is recommended to be used
 	when no CONFIG symbols are involved.
 
-=== 5 Kbuild clean infrastructure
+=== 5 Samples support (uapiprogs-y)
+
+Kbuild support building sample modules and sample binaries.

supports

Documentation/kbuild/makefiles.txt Both possibilities are described in the following. · line 772

Quoted 4 lines
Documentation/kbuild/makefiles.txt +4 -0
+To build sample modules the existing infrastructure is used, but
+to build sample binaries kbuild adds dedicated suppport.
+
+The sample binaries are build for the same host and bit-size as the kernel.

built

Documentation/kbuild/makefiles.txt Both possibilities are described in the following. · line 775

Quoted 3 lines
Documentation/kbuild/makefiles.txt +3 -0
+
+The samples may demonstrate facilities not yet available
+in the installed libc therefore they are build so they include

libc; built

Quoted 30 lines
Documentation/kbuild/makefiles.txt +27 -0
+headers from the exported uapi headers before the libc headers
+are searched.
+
+The sample binaries are usually placed in sub-directories
+below samples/ and specified in samples/Makefile.
+The directories containing sample binaries are listed using
+subdir-y - usually like this: subdir-$(CONFIG_SAMPLES) += dir
+
+The individual binaries may be defined as single .c file per binary
+or several .c files for a single binary.
+See the following examples.
+
+	Example:
+		# samples/hid/Makefile
+		uapiprogs-y := hid-example
+
+	This will compile hid-example.c and create an executable named hid-example.
+
+	Example:
+		# samples/seccomp/Makefile
+		uapiprogs-y := bpf-fancy
+		bpf-fancy-y := bpf-fancy.o bpf-helper.o
+
+This will compile bpf-fancy.c and bpf-helper.c, and then link the executable
+bpf-fancy, based on bpf-fancy.o bpf-helper.o.
+
+=== 6 Kbuild clean infrastructure
 
 "make clean" deletes most generated files in the obj tree where the kernel
 is compiled. This includes generated files such as host programs.
Signature · 1 line
~Randy
#15 lore raw

Re: [PATCH 1/6] kbuild: add support for building userspace api programs

Hi Randy.

Thanks, I will include all corrections in v3.

Sam

#16 lore raw
In reply to Sam Ravnborg

Hi Sam,

Quoted 3 lines · On Sun, 13 Jul 2014 20:36:37 +0200 Sam Ravnborg <[email protected]> wrote:

NOTE: This will likely break the build if the toolchain used do not include a libc. Let's see how much noise this creates.

Then I will drop them from linux-next. Until someone supplies lots of cross compilers with libc support, the samples can not (must not) be built by default.

Signature · 2 lines
Cheers,
Stephen Rothwell                    [email protected]
#17 lore raw

Hi Sam,

Quoted 9 lines · On Mon, 14 Jul 2014 10:29:36 +1000 Stephen Rothwell <[email protected]>…
Quoted 3 lines · On Sun, 13 Jul 2014 20:36:37 +0200 Sam Ravnborg <[email protected]> wrote:

NOTE: This will likely break the build if the toolchain used do not include a libc. Let's see how much noise this creates.

Then I will drop them from linux-next. Until someone supplies lots of cross compilers with libc support, the samples can not (must not) be built by default.

Just to be clear: for my current build infrastructure, this means powerpc and x86_64 hosted cross compilers.

Signature · 2 lines
Cheers,
Stephen Rothwell                    [email protected]
#18 lore raw
Quoted 9 lines · Stephen Rothwell

Hi Sam,

Quoted 6 lines · On Mon, 14 Jul 2014 10:29:36 +1000 Stephen Rothwell <[email protected]>…
Quoted 3 lines · On Sun, 13 Jul 2014 20:36:37 +0200 Sam Ravnborg <[email protected]> wrote:

NOTE: This will likely break the build if the toolchain used do not include a libc. Let's see how much noise this creates.

Quoted 6 lines
Quoted 3 lines

Then I will drop them from linux-next. Until someone supplies lots of cross compilers with libc support, the samples can not (must not) be built by default.

Just to be clear: for my current build infrastructure, this means powerpc and x86_64 hosted cross compilers.

So we basically can have zero, one or both of
A) target toolchain with libc support
B) host toolchain with recent enough kernel headers

Just can idea - in case we have B, but not A, would it make sense to build $(uapiprogs-y) with the host compiler, but without the -Iusr/include flag? The question is whether to add a kconfig knob for this, or some automatic detection for libc support of the target toolchain. I'll come up with a patch later.

Michal

#19 lore raw
Quoted 17 lines · Michal Marek
Quoted 9 lines · Stephen Rothwell

Hi Sam,

Quoted 6 lines · On Mon, 14 Jul 2014 10:29:36 +1000 Stephen Rothwell <[email protected]>…
Quoted 3 lines · On Sun, 13 Jul 2014 20:36:37 +0200 Sam Ravnborg <[email protected]> wrote:

NOTE: This will likely break the build if the toolchain used do not include a libc. Let's see how much noise this creates.

Quoted 6 lines
Quoted 3 lines

Then I will drop them from linux-next. Until someone supplies lots of cross compilers with libc support, the samples can not (must not) be built by default.

Just to be clear: for my current build infrastructure, this means powerpc and x86_64 hosted cross compilers.

Quoted 7 lines

So we basically can have zero, one or both of
A) target toolchain with libc support
B) host toolchain with recent enough kernel headers

Just can idea - in case we have B, but not A, would it make sense to build $(uapiprogs-y) with the host compiler, but without the -Iusr/include flag?

Actually, we can build with the host compiler against the exported headers, these just need to be exported for the right architecture. How about the following patch (applies on top of 1/6 of
this series)? It creates a usr/host/include tree with headers for the host architecture and builds the uapiprogs with the host compiler. This should build in all scenarios that work today. The only downside is that when cross-compiling, the sample binaries continue to be somewhat impractical. But it is not _that_ difficult to build them by hand, provided one has a complete cross-compiling setup.

Michal

Documentation/kbuild/kbuild.txt +0 -5
index 6b691d3b..6466704 100644
@@ -233,8 +233,3 @@ KBUILD_VMLINUX_MAIN
 All object files for the main part of vmlinux.
 KBUILD_VMLINUX_INIT and KBUILD_VMLINUX_MAIN together specify
 all the object files used to link vmlinux.
-
-UAPICFLAGS
---------------------------------------------------
-Arch specific flags used when building sample binaries.
-This is usually the machine size (32 versus 64 bit).
Documentation/kbuild/makefiles.txt +1 -1
index 2c6d53b..163d5dd 100644
@@ -746,7 +746,7 @@ Kbuild support building sample modules and sample binaries.
 To build sample modules the existing infrastructure is used, but
 to build sample binaries kbuild adds dedicated suppport.
 
-The sample binaries are build for the same host and bit-size as the kernel.
+The sample binaries are build for the architecture of the build host.
 
 The samples may demonstrate facilities not yet available
 in the installed libc therefore they are build so they include
Makefile +4 -2
index 9f654a4..66073f2 100644
@@ -968,11 +968,13 @@ prepare1: prepare2 $(version_h) include/generated/utsrelease.h \
 
 archprepare: archheaders archscripts prepare1 scripts_basic
 
-# To build userspace samples the uapi headers needs to be exported
+# To build userspace samples the uapi headers need to be exported for
+# the host architecture
 prepare0: archprepare FORCE
 	$(Q)$(MAKE) $(build)=.
 ifdef CONFIG_SAMPLES
-	$(Q)$(MAKE) KBUILD_SRC= headers_install
+	$(MAKE) KBUILD_SRC= ARCH=$(SUBARCH) \
+		INSTALL_HDR_PATH=usr/host headers_install
 endif
 
 # All the preparing..
arch/s390/Makefile +0 -2
index 4b32933..874e6d6 100644
@@ -18,7 +18,6 @@ LD_BFD		:= elf32-s390
 LDFLAGS		:= -m elf_s390
 KBUILD_CFLAGS	+= -m31
 KBUILD_AFLAGS	+= -m31
-UAPICFLAGS	+= -m31
 UTS_MACHINE	:= s390
 STACK_SIZE	:= 8192
 CHECKFLAGS	+= -D__s390__ -msize-long
@@ -29,7 +28,6 @@ KBUILD_AFLAGS_MODULE += -fPIC
 KBUILD_CFLAGS_MODULE += -fPIC
 KBUILD_CFLAGS	+= -m64
 KBUILD_AFLAGS	+= -m64
-UAPICFLAGS	+= -m64
 UTS_MACHINE	:= s390x
 STACK_SIZE	:= 16384
 CHECKFLAGS	+= -D__s390__ -D__s390x__
arch/sparc/Makefile +0 -2
index fc0dcf1..eaee146 100644
@@ -26,7 +26,6 @@ UTS_MACHINE    := sparc
 
 KBUILD_CFLAGS  += -m32 -mcpu=v8 -pipe -mno-fpu -fcall-used-g5 -fcall-used-g7
 KBUILD_AFLAGS  += -m32 -Wa,-Av8
-UAPICFLAGS     += -m32
 
 else
 #####
@@ -43,7 +42,6 @@ KBUILD_CFLAGS += -ffixed-g4 -ffixed-g5 -fcall-used-g7 -Wno-sign-compare
 KBUILD_CFLAGS += -Wa,--undeclared-regs
 KBUILD_CFLAGS += $(call cc-option,-mtune=ultrasparc3)
 KBUILD_AFLAGS += -m64 -mcpu=ultrasparc -Wa,--undeclared-regs
-UAPICFLAGS    += -m64
 
 ifeq ($(CONFIG_MCOUNT),y)
   KBUILD_CFLAGS += -pg
arch/x86/Makefile +0 -2
index add7271..c1aa368 100644
@@ -47,7 +47,6 @@ ifeq ($(CONFIG_X86_32),y)
         biarch := $(call cc-option,-m32)
         KBUILD_AFLAGS += $(biarch)
         KBUILD_CFLAGS += $(biarch)
-        UAPICFLAGS    += $(biarch)
 
         KBUILD_CFLAGS += -msoft-float -mregparm=3 -freg-struct-return
 
@@ -80,7 +79,6 @@ else
         biarch := -m64
         KBUILD_AFLAGS += -m64
         KBUILD_CFLAGS += -m64
-        UAPICFLAGS    += -m64
 
         # Don't autogenerate traditional x87, MMX or SSE instructions
         KBUILD_CFLAGS += -mno-mmx -mno-sse
scripts/Makefile.uapiprogs +4 -5
index 63c09d0..c88a0cc 100644
@@ -29,8 +29,7 @@ uapi-cmulti  := $(addprefix $(obj)/,$(uapi-cmulti))
 uapi-cobjs   := $(addprefix $(obj)/,$(uapi-cobjs))
 
 # Options to uapicc.
-uapic_flags = -Wp,-MD,$(depfile) -isystem $(INSTALL_HDR_PATH)/include \
-              $(UAPICFLAGS) -Wall
+uapic_flags = -Wp,-MD,$(depfile) -isystem usr/host/include $(HOSTCFLAGS)
 
 #####
 # Compile uapi programs on the build host
@@ -38,21 +37,21 @@ uapic_flags = -Wp,-MD,$(depfile) -isystem $(INSTALL_HDR_PATH)/include \
 # Create executable from a single .c file
 # uapi-csingle -> executable
 quiet_cmd_uapi-csingle = UAPICC  $@
-      cmd_uapi-csingle = $(CC) $(uapic_flags) -o $@ $<
+      cmd_uapi-csingle = $(HOSTCC) $(uapic_flags) -o $@ $<
 $(uapi-csingle): $(obj)/%: $(src)/%.c FORCE
 	$(call if_changed_dep,uapi-csingle)
 
 # Create .o file from a single .c file
 # uapi-cobjs -> .o
 quiet_cmd_uapi-cobjs	= UAPICC  $@
-      cmd_uapi-cobjs	= $(CC) $(uapic_flags) -c -o $@ $<
+      cmd_uapi-cobjs	= $(HOSTCC) $(uapic_flags) -c -o $@ $<
 $(uapi-cobjs): $(obj)/%.o: $(src)/%.c FORCE
 	$(call if_changed_dep,uapi-cobjs)
 
 # Link an executable based on list of .o files
 # uapi-cmulti -> executable
 quiet_cmd_uapi-cmulti	= UAPILD  $@
-      cmd_uapi-cmulti	= $(CC) -o $@ \
+      cmd_uapi-cmulti	= $(HOSTCC) -o $@ \
 			  $(addprefix $(obj)/,$($(@F)-y))
 $(uapi-cmulti): $(obj)/%: $(uapi-cobjs) FORCE
 	$(call if_changed,uapi-cmulti)