/ src / Makefile.modinc.in
Makefile.modinc.in
  1  # Makefile.modinc includes rules for building HAL realtime modules outside
  2  # the LinuxCNC source tree.  It has three useful targets:
  3  #
  4  # modules
  5  #   Actually build the modules
  6  #
  7  # clean
  8  #   Cleans up files made by 'modules'
  9  #
 10  # install
 11  #   Installs the modules
 12  
 13  # An example Makefile using Makefile.modinc to build one kernel module from a
 14  # single source file would read:
 15  # 
 16  # obj-m += example.o
 17  # include .../Makefile.modinc
 18  
 19  # An example Makefile using Makefile.modinc to build one kernel module from
 20  # several source files would read:
 21  # 
 22  # obj-m += complex.o
 23  # complex-objs := complex1.o complex2.o complex_main.o
 24  # include .../Makefile.modinc
 25  
 26  # Currently this Makefile is only suitable for 'kbuild' and 'uspace' systems,
 27  # but there is no technical reason it cannot be extended to pre-kbuild systems.
 28  
 29  # When there is a single module and it consists of a single source file, an
 30  # easier way to build modules is to invoke 'comp':
 31  #  comp --compile example.c
 32  # or
 33  #  comp --install example.c
 34  
 35  ifeq ("$(origin V)", "command line")
 36    BUILD_VERBOSE = $(V)
 37  endif
 38  ifndef BUILD_VERBOSE
 39    BUILD_VERBOSE = 0
 40  endif
 41  
 42  ifeq ($(BUILD_VERBOSE),1)
 43    Q =
 44  else
 45    Q = @
 46  endif
 47  
 48  ifeq "$(findstring s,$(MAKEFLAGS))" ""
 49  ECHO=@echo
 50  VECHO=echo
 51  else
 52  ECHO=@true
 53  VECHO=true
 54  endif
 55  
 56  
 57  cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
 58               > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 59  
 60  BUILDSYS = @BUILD_SYS@
 61  
 62  KERNELDIR := @KERNELDIR@
 63  CC := @CC@
 64  RTAI = @RTAI@
 65  RTFLAGS = $(filter-out -ffast-math,@RTFLAGS@ @EXT_RTFLAGS@) -fno-fast-math $(call cc-option,-mieee-fp) -fno-unsafe-math-optimizations
 66  RTFLAGS := -Os -g -I. -I@RTDIR@/include $(RTFLAGS) -DRTAPI -D_GNU_SOURCE -Drealtime
 67  ifneq ($(KERNELRELEASE),)
 68  ifeq ($(RTARCH):$(RTAI):$(filter $(RTFLAGS),-msse),x86_64:3:)
 69  EXTRA_CFLAGS += -msse
 70  endif
 71  endif
 72  USE_RTLIBM = @USE_RTLIBM@
 73  EMC2_HOME=@EMC2_HOME@
 74  RUN_IN_PLACE=@RUN_IN_PLACE@
 75  ifeq ($(RUN_IN_PLACE),yes)
 76  EXTRA_CFLAGS := $(RTFLAGS) -D__MODULE__ -I$(EMC2_HOME)/include
 77  RTLIBDIR := @EMC2_HOME@/rtlib
 78  LIBDIR := @EMC2_HOME@/lib
 79  else
 80  prefix := @prefix@
 81  EXTRA_CFLAGS := $(RTFLAGS) -D__MODULE__ -I@includedir@/linuxcnc
 82  RTLIBDIR := @EMC2_RTLIB_DIR@
 83  LIBDIR := @libdir@
 84  endif
 85  EXTRA_CFLAGS += -Wframe-larger-than=2560
 86  
 87  ifeq ($(BUILDSYS),kbuild)
 88  modules:
 89  	$(MAKE) KBUILD_EXTRA_SYMBOLS=$(RTLIBDIR)/Module.symvers -C $(KERNELDIR) SUBDIRS=`pwd` CC=$(CC) V=0 modules
 90  
 91  clean:
 92  	rm *.ko *.mod.c *.o
 93  
 94  install:
 95  	cp $(patsubst %.o,%.ko,$(obj-m)) $(DESTDIR)$(RTLIBDIR)/
 96  endif
 97  
 98  ifeq ($(BUILDSYS),uspace)
 99  EXTRA_CFLAGS += -DSIM -fPIC
100  allmodules = $(patsubst %.o,%.so,$(obj-m))
101  modules: $(allmodules)
102  
103  install: modules
104  	cp $(allmodules) $(DESTDIR)$(RTLIBDIR)/
105  
106  getobjs = $(if $(filter undefined, $(origin $(1)-objs)), $(1).o, $($(1)-objs))
107  getsrcs = $(patsubst %.o,%.c,$(call getobjs,$(1)))
108  
109  maks := $(patsubst %.o,.%.modinc,$(obj-m))
110  $(foreach mod,$(patsubst %.o,%,$(obj-m)),\
111  	$(eval $(mod).so: $(call getobjs,$(mod))))
112  %.o: %.c
113  	$(ECHO) Compiling realtime $<
114  	$(Q)$(CC) -o $@ $(EXTRA_CFLAGS) -c $<
115  
116  .PHONY: %.so
117  %.so:
118  	$(ECHO) Linking $@
119  	$(Q)ld -d -r -o $*.tmp $^
120  	$(Q)objcopy -j .rtapi_export -O binary $*.tmp $*.sym
121  	$(Q)(echo '{ global : '; tr -s '\0' < $*.sym | xargs -r0 printf '%s;\n' | grep .; echo 'local : * ; };') > $*.ver
122  	$(Q)$(CC) -shared -Bsymbolic $(LDFLAGS) -Wl,--version-script,$*.ver -o $@ $^ -lm
123  endif
124  
125  ifeq ($(BUILDSYS),normal)
126  $(error Makefile.modinc is only suitable for 'kbuild' kernels and 'uspace' systems)
127  endif
128