summaryrefslogtreecommitdiff
path: root/src/Makefile.modinc.in
blob: 9df6b6dad976033ce936349912d22af4e6154b7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Makefile.modinc includes rules for building HAL realtime modules outside
# the LinuxCNC source tree.  It has three useful targets:
#
# modules
#   Actually build the modules
#
# clean
#   Cleans up files made by 'modules'
#
# install
#   Installs the modules

# An example Makefile using Makefile.modinc to build one kernel module from a
# single source file would read:
# 
# obj-m += example.o
# include .../Makefile.modinc

# An example Makefile using Makefile.modinc to build one kernel module from
# several source files would read:
# 
# obj-m += complex.o
# complex-objs := complex1.o complex2.o complex_main.o
# include .../Makefile.modinc

# Currently this Makefile is only suitable for 'kbuild' and 'sim' systems, but
# there is no technical reason it cannot be extended to pre-kbuild systems.

# When there is a single module and it consists of a single source file, an
# easier way to build modules is to invoke 'comp':
#  comp --compile example.c
# or
#  comp --install example.c

ifeq ("$(origin V)", "command line")
  BUILD_VERBOSE = $(V)
endif
ifndef BUILD_VERBOSE
  BUILD_VERBOSE = 0
endif

ifeq ($(BUILD_VERBOSE),1)
  Q =
else
  Q = @
endif

ifeq "$(findstring s,$(MAKEFLAGS))" ""
ECHO=@echo
VECHO=echo
else
ECHO=@true
VECHO=true
endif


cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
             > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)

BUILDSYS = @BUILD_SYS@

KERNELDIR := @KERNELDIR@
CC := @CC@
RTAI = @RTAI@
RTFLAGS = $(filter-out -ffast-math,@RTFLAGS@ @EXT_RTFLAGS@) -fno-fast-math $(call cc-option,-mieee-fp) -fno-unsafe-math-optimizations
RTFLAGS := -Os -g -I. -I@RTDIR@/include $(RTFLAGS) -DRTAPI -D_GNU_SOURCE -Drealtime
ifneq ($(KERNELRELEASE),)
ifeq ($(RTARCH):$(RTAI):$(filter $(RTFLAGS),-msse),x86_64:3:)
EXTRA_CFLAGS += -msse
endif
endif
USE_RTLIBM = @USE_RTLIBM@
EMC2_HOME=@EMC2_HOME@
RUN_IN_PLACE=@RUN_IN_PLACE@
ifeq ($(RUN_IN_PLACE),yes)
EXTRA_CFLAGS := $(RTFLAGS) -D__MODULE__ -I$(EMC2_HOME)/include
RTLIBDIR := @EMC2_HOME@/rtlib
LIBDIR := @EMC2_HOME@/lib
else
prefix := @prefix@
EXTRA_CFLAGS := $(RTFLAGS) -D__MODULE__ -I@includedir@/linuxcnc
RTLIBDIR := @EMC2_RTLIB_DIR@
LIBDIR := @libdir@
endif
EXTRA_CFLAGS += $(call cc-option,-Wframe-larger-than=2560)

ifeq ($(BUILDSYS),kbuild)
modules:
	$(MAKE) KBUILD_EXTRA_SYMBOLS=$(RTLIBDIR)/Module.symvers -C $(KERNELDIR) SUBDIRS=`pwd` CC=$(CC) V=0 modules

clean:
	rm *.ko *.mod.c *.o

install:
	cp $(patsubst %.o,%.ko,$(obj-m)) $(DESTDIR)$(RTLIBDIR)/
endif

ifeq ($(BUILDSYS),sim)
EXTRA_CFLAGS += -DSIM -fPIC
allmodules = $(patsubst %.o,%.so,$(obj-m))
modules: $(allmodules)

install: modules
	cp $(allmodules) $(DESTDIR)$(RTLIBDIR)/

getobjs = $(if $(filter undefined, $(origin $(1)-objs)), $(1).o, $($(1)-objs))
getsrcs = $(patsubst %.o,%.c,$(call getobjs,$(1)))

maks := $(patsubst %.o,.%.modinc,$(obj-m))
$(foreach mod,$(patsubst %.o,%,$(obj-m)),\
	$(eval $(mod).so: $(call getobjs,$(mod))))
%.o: %.c
	$(ECHO) Compiling realtime $<
	$(Q)$(CC) -o $@ $(EXTRA_CFLAGS) -c $<

IS_POWERPC = test `uname -m` = ppc -o `uname -m` = ppc64

.PHONY: %.so
%.so:
	$(ECHO) Linking $@
	$(Q)ld -d -r -o $*.tmp $^
	$(Q)if ! $(IS_POWERPC); then objcopy -j .rtapi_export -O binary $*.tmp $*.exp; fi
	$(Q)if ! $(IS_POWERPC); then objcopy -G __x86.get_pc_thunk.bx -G __i686.get_pc_thunk.bx `xargs -r0n1 echo -G < $*.exp | grep -ve '^-G $$' | sort -u` $*.tmp; fi
	$(Q)$(CC) -shared -Bsymbolic $(LDFLAGS) -o $@ $*.tmp -lm
endif

ifeq ($(BUILDSYS),normal)
$(error Makefile.modinc is only suitable for 'kbuild' kernels and 'sim' systems)
endif