From e742f1649eb39ecc0cbdd053207830de368948e1 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Fri, 13 Feb 2015 00:55:02 +0100 Subject: [PATCH] Include host tool to send code and FPGA configuration to LX_CPU board. Signed-off-by: Pavel Pisa --- host/.gitignore | 5 + host/Makefile | 14 + host/Makefile.omk | 5 + host/Makefile.rules | 1453 ++++++++++++++++++++++++++++ host/app/Makefile | 14 + host/app/Makefile.omk | 1 + host/app/usb_sendhex/Makefile | 14 + host/app/usb_sendhex/Makefile.omk | 11 + host/app/usb_sendhex/usb_sendhex.c | 1178 ++++++++++++++++++++++ host/config.omk | 12 + 10 files changed, 2707 insertions(+) create mode 100644 host/.gitignore create mode 100644 host/Makefile create mode 100644 host/Makefile.omk create mode 100644 host/Makefile.rules create mode 100644 host/app/Makefile create mode 100644 host/app/Makefile.omk create mode 100644 host/app/usb_sendhex/Makefile create mode 100644 host/app/usb_sendhex/Makefile.omk create mode 100644 host/app/usb_sendhex/usb_sendhex.c create mode 100644 host/config.omk diff --git a/host/.gitignore b/host/.gitignore new file mode 100644 index 0000000..5040514 --- /dev/null +++ b/host/.gitignore @@ -0,0 +1,5 @@ +_build +_compiled +config.omk-default +sources.txt +tags diff --git a/host/Makefile b/host/Makefile new file mode 100644 index 0000000..b22a357 --- /dev/null +++ b/host/Makefile @@ -0,0 +1,14 @@ +# Generic directory or leaf node makefile for OCERA make framework + +ifndef MAKERULES_DIR +MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) ) +endif + +ifeq ($(MAKERULES_DIR),) +all : default +.DEFAULT:: + @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n" +else +include $(MAKERULES_DIR)/Makefile.rules +endif + diff --git a/host/Makefile.omk b/host/Makefile.omk new file mode 100644 index 0000000..be5901d --- /dev/null +++ b/host/Makefile.omk @@ -0,0 +1,5 @@ +# -*- makefile -*- + +SUBDIRS = app + +-include $(SOURCES_DIR)/Makefile.omk-additional diff --git a/host/Makefile.rules b/host/Makefile.rules new file mode 100644 index 0000000..1458619 --- /dev/null +++ b/host/Makefile.rules @@ -0,0 +1,1453 @@ +# Makefile.rules - OCERA make framework common project rules -*- makefile-gmake -*- #OMK:base.omk +# +# (C) Copyright 2003, 2006, 2007, 2008, 2009 by Pavel Pisa - OCERA team member +# (C) Copyright 2006, 2007, 2008, 2009, 2010, 2011 by Michal Sojka - Czech Technical University, FEE, DCE +# +# Homepage: http://rtime.felk.cvut.cz/omk/ +# Version: 0.2-97-g532aeb1 +# +# The OMK build system is distributed under the GNU General Public +# License. See file COPYING for details. +# +# +# Version for Linux/RTLinux builds. #OMK:linux.omk +# +# +# input variables #OMK:base.omk +# V .. if set to 1, full command text is shown else short form is used +# W .. whole tree - if set to 1, make is always called from the top-level directory +# SUBDIRS .. list of subdirectories intended for make from actual directory +# default_CONFIG .. list of default config assignments CONFIG_XXX=y/n ... +# wvtest_SCRIPTS .. list of scripts producing wvtest output #OMK:wvtest.omk +# wvtest_PROGRAMS .. list of the testing programs producing wvtest output +# LN_HEADERS .. if "y", header files are symbolicaly linked instead of copied. #OMK:include.omk +# input variables #OMK:linux.omk +# lib_LIBRARIES .. list of the user-space libraries +# shared_LIBRARIES .. list of the user-space shared libraries +# kernel_LIBRARIES .. list of the kernel-space libraries +# rtlinux_LIBRARIES.. list of the RT-Linux kernel-space libraries +# include_HEADERS .. list of the user-space public header files +# nobase_include_HEADERS .. public headers copied even with directory part +# renamed_include_HEADERS .. public headers copied to the different target name +# kernel_HEADERS .. list of the kernel-space public header files +# rtlinux_HEADERS .. list of the RT-Linux kernel-space public header files +# bin_PROGRAMS .. list of the require binary programs +# utils_PROGRAMS .. list of the development utility programs +# test_PROGRAMS .. list of the testing programs +# bin_SCRIPTS .. list of scripts to be copied to _compiled/bin +# kernel_MODULES .. list of the kernel side modules/applications +# rtlinux_MODULES .. list of RT-Linux the kernel side modules/applications +# xxx_SOURCES .. list of specific target sources +# xxx_LIBS .. list of specific target libraries (-l prefix is automatically added) +# xxx_LDFLAGS .. list of specific target LDFLAGS +# lib_LOADLIBES .. list of libraries linked to each executable +# INCLUDES .. additional include directories and defines for user-space +# kernel_INCLUDES .. additional include directories and defines for kernel-space +# rtlinux_INCLUDES .. additional include directories and defines for RT-Linux +# OMIT_KERNEL_PASSES if defined, all kernel passes are omited +# +# LINUX_DIR .. location of Linux kernel sources +# RTL_DIR .. location of RT-Linux sources +# CFLAGS .. C compiler flags +# CXXFLAGS .. C++ compiler flags +# CPPFLAGS .. C preprocessor flags +# LDFLAGS .. linker flags for programs linking +# LOCAL_CONFIG_H .. name of local config.h file generated from values #OMK:config_h.omk +# of options defined in the current directory +# config_include_HEADERS .. names of global config files (possibly +# with subdirectories) +# xxx_DEFINES .. list of config directives to be included in +# config header file of the name /xxx.h +# DOXYGEN .. if non-empty, generated headers includes Doxygen's @file +# command, so it is possible to document config +# variables. +# QT_PROJECTS .. list of QT .pro file to use for compilation #OMK:qt.omk +# QT_SUBDIRS .. subdirectories where to build QT applications using qmake (depricated) +# QTDIR .. where QT resides +OMK_RULES_TYPE=linux #OMK:Makefile.rules.linux@ + #OMK:base.omk@Makefile.rules.linux +# If we are not called by OMK leaf Makefile... +ifndef MAKERULES_DIR +MAKERULES_DIR := $(abspath $(dir $(filter %Makefile.rules,$(MAKEFILE_LIST)))) +endif + +# The $(SED4OMK) command for BSD based systems requires -E option to allow +# extended regular expressions + +SED4OMK ?= sed +ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y) + SED4OMK := $(SED4OMK) -E + ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y) + SED4OMK := gsed + endif + ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y) + SED4OMK := gsed -E + endif + ifneq ($(shell ( echo A | $(SED4OMK) -n -e 's/A\|B/y/p' )),y) + $(error No SED program suitable for OMK found) + endif +endif + +# OUTPUT_DIR is the place where _compiled, _build and possible other +# files/directories are created. By default is the same as +# $(MAKERULES_DIR). +ifndef OUTPUT_DIR +OUTPUT_DIR := $(MAKERULES_DIR) +endif + +# We need to ensure definition of sources directory first +ifndef SOURCES_DIR +# Only shell built-in pwd understands -L +SOURCES_DIR := $(shell ( pwd -L ) ) +INVOCATION_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%) +INVOCATION_DIR := $(INVOCATION_DIR:/%=%) +INVOCATION_DIR := $(INVOCATION_DIR:\\%=%) +endif + +.PHONY: all default check-make-ver print-hints omkize + +ifdef W + ifeq ("$(origin W)", "command line") + OMK_WHOLE_TREE:=$(W) + endif +endif +ifndef OMK_WHOLE_TREE + OMK_WHOLE_TREE:=0 +endif + +ifneq ($(OMK_WHOLE_TREE),1) +all: check-make-ver print-hints default + @echo "Compilation finished" +else +# Run make in the top-level directory +all: + @$(MAKE) -C $(MAKERULES_DIR) OMK_SERIALIZE_INCLUDED=n SOURCES_DIR=$(MAKERULES_DIR) RELATIVE_DIR="" $(MAKECMDGOALS) W=0 +endif + +# omk-get-var target allows external scripts/programs to determine the +# values of OMK variables such as RELATIVE_DIR etc. +.PHONY: omk-get-var +omk-get-var: + @$(foreach var,$(VAR),echo $(var)=$($(var));) + +#========================= +# Include the config file + +ifndef CONFIG_FILE +CONFIG_FILE := $(OUTPUT_DIR)/config.omk +endif + +$(CONFIG_FILE)-default: + $(MAKE) default-config + +ifeq ($(MAKECMDGOALS),default-config) +export DEFAULT_CONFIG_PASS=1 +endif + +ifneq ($(DEFAULT_CONFIG_PASS),1) +include $(CONFIG_FILE)-default +endif + +-include $(OUTPUT_DIR)/config.target + +ifneq ($(wildcard $(CONFIG_FILE)),) +-include $(CONFIG_FILE) +endif + + +CONFIG_FILES ?= $(wildcard $(CONFIG_FILE)-default) $(wildcard $(OUTPUT_DIR)/config.target) $(wildcard $(CONFIG_FILE)) + + +export SED4OMK SOURCES_DIR MAKERULES_DIR RELATIVE_DIR INVOCATION_DIR +export CONFIG_FILE CONFIG_FILES OMK_SERIALIZE_INCLUDED OMK_VERBOSE OMK_SILENT +# OMK_SERIALIZE_INCLUDED has to be exported to submakes because passes +# must to be serialized only in the toplevel make. + +ifndef RELATIVE_DIR +RELATIVE_DIR := $(SOURCES_DIR:$(OUTPUT_DIR)%=%) +endif +#$(warning === RELATIVE_DIR = "$(RELATIVE_DIR)" ===) +override RELATIVE_DIR := $(RELATIVE_DIR:/%=%) +override RELATIVE_DIR := $(RELATIVE_DIR:\\%=%) +#$(warning RELATIVE_DIR = "$(RELATIVE_DIR)") +#override BACK2TOP_DIR := $(shell echo $(RELATIVE_DIR)/ | $(SED4OMK) -e 's_//_/_g' -e 's_/\./_/_g' -e 's_^\./__g' -e 's_\([^/][^/]*\)_.._g' -e 's_/$$__') +#$(warning BACK2TOP_DIR = "$(BACK2TOP_DIR)") + +#$(warning SOURCES_DIR = "$(SOURCES_DIR)") +#$(warning MAKERULES_DIR = "$(OUTPUT_DIR)") +#$(warning RELATIVE_DIR = "$(RELATIVE_DIR)") + +# We have to use RELATIVE_PREFIX because of mingw +override RELATIVE_PREFIX := $(RELATIVE_DIR)/ +override RELATIVE_PREFIX := $(RELATIVE_PREFIX:/%=%) + +#vpath %.c $(SOURCES_DIR) +#vpath %.cc $(SOURCES_DIR) +#vpath %.cxx $(SOURCES_DIR) + +# Define srcdir for Automake compatibility +srcdir = $(SOURCES_DIR) + +# Defines for quiet compilation +ifdef V + ifeq ("$(origin V)", "command line") + OMK_VERBOSE = $(V) + endif +endif +ifndef OMK_VERBOSE + OMK_VERBOSE = 0 +endif +ifneq ($(OMK_VERBOSE),0) + Q = +else + Q = @ +endif +ifneq ($(findstring s,$(MAKEFLAGS)),) + QUIET_CMD_ECHO = true + OMK_SILENT = 1 +else + QUIET_CMD_ECHO = echo +endif + +MAKEFILE_OMK=Makefile.omk +# All subdirectories (even linked ones) containing Makefile.omk +# Usage in Makefile.omk: SUBDIRS = $(ALL_OMK_SUBDIRS) +ALL_OMK_SUBDIRS = $(patsubst %/$(MAKEFILE_OMK),%,$(patsubst $(SOURCES_DIR)/%,%,$(wildcard $(SOURCES_DIR)/*/$(MAKEFILE_OMK)))) + +# =================================================================== +# We have set up all important variables, so we can check and include +# real OCERA style Makefile.omk now +ifndef OMK_INCLUDED +include $(SOURCES_DIR)/$(MAKEFILE_OMK) +ifeq ($(AUTOMATIC_SUBDIRS),y) +SUBDIRS?=$(ALL_OMK_SUBDIRS) +endif +OMK_INCLUDED := 1 +endif + +print-hints: + @echo 'Use "make V=1" to see the verbose compile lines.' + +check-make-ver: + @GOOD_MAKE_VERSION=`echo $(MAKE_VERSION) | $(SED4OMK) -n -e 's/^[4-9]\..*\|^3\.9[0-9].*\|^3\.8[1-9].*/y/p'` ; \ + if [ x$$GOOD_MAKE_VERSION != xy ] ; then \ + echo "Your make program version ($(MAKE_VERSION)) is too old and does not support OMK system." ; \ + echo "Please update to make program 3.81beta1 or newer." ; exit 1 ; \ + fi + +distclean dist-clean: + @$(QUIET_CMD_ECHO) " RM $(COMPILED_DIR_NAME) $(BUILD_DIR_NAME)" + @rm -fr $(OUTPUT_DIR)/$(COMPILED_DIR_NAME) $(OUTPUT_DIR)/$(BUILD_DIR_NAME) + +# Common OMK templates +# ==================== + +# Syntax: $(call mkdir,) +define mkdir_def + [ -d $(1) ] || mkdir -p $(1) || exit 1 +endef + +ifneq ($(OMK_VERBOSE),2) +NO_PRINT_DIRECTORY := --no-print-directory +endif + +ifeq ($(USE_LEAF_MAKEFILES),n) +export USE_LEAF_MAKEFILES +SUBDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules +SOURCESDIR_MAKEFILE=$(MAKERULES_DIR)/Makefile.rules +else +SUBDIR_MAKEFILE=$(SOURCES_DIR)/$(3)/Makefile +SOURCESDIR_MAKEFILE=$(SOURCES_DIR)/Makefile +endif + +pass = $(strip $(1)) + +unexport SUBDIRS + +# Call a pass in a subdirectory +# Usage: $(call omk_pass_subdir_template,,,) +define omk_pass_subdir_template +.PHONY: $(pass)-$(3)-subdir +$(pass)-submakes: $(pass)-$(3)-subdir +$(pass)-$(3)-subdir: MAKEOVERRIDES:=$(filter-out SUBDIRS=%,$(MAKEOVERRIDES)) +$(pass)-$(3)-subdir: + @$(call mkdir_def,$(2)/$(3)) + +@$(MAKE) --no-builtin-rules SOURCES_DIR=$(SOURCES_DIR)/$(3) $(NO_PRINT_DIRECTORY) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(3) -C $(2)/$(3) \ + -f $(SUBDIR_MAKEFILE) $(pass)-submakes +# In subdirectories we can call submakes directly since passes are +# already serialized on the toplevel make. +endef + +ifdef OMK_TESTSROOT +check-target = $(1:%=%-check) +endif + +# Call a pass in a subdirectory +# Usage: $(call extra_rules_subdir_template,) +define extra_rules_subdir_template +extra-rules-subdirs: extra-rules-$(1) +extra-rules-$(1): + +@$(MAKE) OMK_SERIALIZE_INCLUDED=n MAKERULES_DIR=$(SOURCES_DIR)/$(1) OUTPUT_DIR=$(OUTPUT_DIR) \ + SOURCES_DIR=$(SOURCES_DIR)/$(1) RELATIVE_DIR=$(RELATIVE_PREFIX)$(1) -C $(SOURCES_DIR)/$(1) +endef + +.PHONY: extra-rules-subdirs +extra-rules-subdirs: + +$(foreach subdir,$(EXTRA_RULES_SUBDIRS),$(eval $(call extra_rules_subdir_template,$(subdir)))) + +# Usage: $(call omk_pass_template,,,[],[]) +define omk_pass_template +.PHONY: $(pass) $(pass)-local $(pass)-check $(pass)-submakes +$(foreach subdir,$(SUBDIRS),$(eval $(call omk_pass_subdir_template,$(pass),$(2),$(subdir)))) +$(pass): +# Submakes have to be called this way and not as dependecies for pass +# serialization to work + +@$(MAKE) --no-builtin-rules SOURCES_DIR=$(SOURCES_DIR) $(NO_PRINT_DIRECTORY) \ + RELATIVE_DIR=$(RELATIVE_DIR) \ + -f $(SOURCESDIR_MAKEFILE) $(pass)-submakes +$(pass)-submakes: + @true # Do not emit "nothing to be done" messages + +ifneq ($(4)$($(pass)_HOOKS),) +$(pass)-submakes: $(pass)-this-dir +$(pass)-this-dir: $(foreach subdir,$(SUBDIRS),$(pass)-$(subdir)-subdir) + +@echo "make[omk]: $(pass) in $(RELATIVE_DIR)" + @$(call mkdir_def,$(2)) + +@$(MAKE) --no-builtin-rules $(NO_PRINT_DIRECTORY) SOURCES_DIR=$(SOURCES_DIR) RELATIVE_DIR=$(RELATIVE_DIR) -C $(2) \ + -f $(SOURCESDIR_MAKEFILE) $(3) $(check-target) $(1:%=%-local) +$(pass)-local: $($(pass)_HOOKS) +endif +endef + +# ======================= +# DEFAULT CONFIG PASS + +default-config: + @echo "# Start of OMK config file" > "$(CONFIG_FILE)-default" + @echo "# This file should not be altered manually" >> "$(CONFIG_FILE)-default" + @echo "# Overrides should be stored in file $(notdir $(CONFIG_FILE))" >> "$(CONFIG_FILE)-default" + @echo >> "$(CONFIG_FILE)-default" + @$(MAKE) $(NO_PRINT_DIRECTORY) -C $(OUTPUT_DIR) \ + RELATIVE_DIR="" SOURCES_DIR=$(OUTPUT_DIR) \ + -f $(OUTPUT_DIR)/Makefile default-config-pass + +$(eval $(call omk_pass_template,default-config-pass,$$(LOCAL_BUILD_DIR),,always)) + +default-config-pass-local: +# @echo Default config for $(RELATIVE_DIR) + @echo "# Config for $(RELATIVE_DIR)" >> "$(CONFIG_FILE)-default" + @$(foreach x, $(default_CONFIG), echo '$(x)' | \ + $(SED4OMK) -e 's/^[^=]*=x$$/#\0/' >> "$(CONFIG_FILE)-default" ; ) + + +omkize: + $(Q)if ! grep -q MAKERULES_DIR Makefile; then \ + echo "Makefile is not OMK leaf makefile!" >&2; exit 1; \ + fi + $(Q)for i in `find -L . -name Makefile.omk` ; do \ + d=`dirname $${i}`; \ + if ! test -f "$${d}/Makefile.rules" && ( test -f "$${d}/Makefile" && ! cmp --silent Makefile "$${d}/Makefile" ); then \ + rm -f "$${d}/Makefile"; \ + cp -v Makefile "$${d}/Makefile"; \ + fi \ + done + #OMK:wvtest.omk@Makefile.rules.linux +ifndef WVTEST_LIBRARY +WVTEST_LIBRARY = wvtest +endif + +# Documentation: wvtest_PROGRAMS is amost equivalent to test_PROGRAMS. +# The two differences are that the program is automatically linked +# with $(WVTEST_LIBRARY) and it is run during "make wvtest". +test_PROGRAMS += $(wvtest_PROGRAMS) + +# Documentation: If your project uses wvtest, it is recomended to put +# the "test: wvtest" rule to config.target. +wvtest: + $(Q)$(MAKERULES_DIR)/wvtestrun $(MAKE) wvtest-pass + +.PHONY: wvtest + +$(eval $(call omk_pass_template,wvtest-pass,$$(LOCAL_BUILD_DIR),,$(wvtest_SCRIPTS)$(wvtest_PROGRAMS))) + +# Usage: $(call wvtest_template,) +define wvtest_template +wvtest-pass-local: wvtest-run-$(1) +.PHONY: wvtest-run-$(1) +wvtest-run-$(1): + $(Q)echo "Testing \"Run $(1)\" in Makefile.rules:" + $(Q)mkdir -p $(1).wvtest + $(Q)cd $(1).wvtest && \ + PATH=$$(USER_BIN_DIR):$$$$PATH LD_LIBRARY_PATH=$$(USER_LIB_DIR):$$$$LD_LIBRARY_PATH \ + $(1) +$(notdir $(1))_LIBS += $$(WVTEST_LIBRARY) +endef + +# Documentation: Write the test so, that it can be run from arbitrary +# directory, i.e. in case of a script ensure that the wvtest library +# is sourced like this: +# +# . $(dirname $0)/wvtest.sh + +$(foreach script,$(wvtest_SCRIPTS),$(eval $(call wvtest_template,$(SOURCES_DIR)/$(script)))) +# Hack!!! +USER_TESTS_DIR := $(OUTPUT_DIR)/_compiled/bin-tests +$(foreach prog,$(wvtest_PROGRAMS),$(eval $(call wvtest_template,$(USER_TESTS_DIR)/$(prog)))) +ifeq ($(OMK_VERBOSE),1) #OMK:include.omk@Makefile.rules.linux +CPHEADER_FLAGS += -v +LNHEADER_FLAGS += -v +endif + +ifneq ($(LN_HEADERS),y) +define cp_cmd +if ! cmp --quiet $(1) $(2); then \ + echo " CP $(1:$(OUTPUT_DIR)/%=%) -> $(2:$(OUTPUT_DIR)/%=%)"; \ + install -D $(CPHEADER_FLAGS) $(1) $(2) || exit 1; \ +fi +endef +else +define cp_cmd +if ! cmp --quiet $(1) $(2); then \ + echo " LN $(1:$(OUTPUT_DIR)/%=%) -> $(2:$(OUTPUT_DIR)/%=%)"; \ + if [ -f $(1) ]; then d=$(2); mkdir -p $${d%/*} && ln -sf $(LNHEADER_FLAGS) $(1) $(2) || exit 1; else exit 1; fi; \ +fi +endef +endif + +# TODO: Check modification date of changed header files. If it is +# newer that in source dir, show a warning. + +# Syntax: $(call include-pass-template,,) +define include-pass-template +include-pass-local: include-pass-local-$(2) +include-pass-local-$(2): $$($(2)_GEN_HEADERS) $$(foreach f,$$(renamed_$(2)_GEN_HEADERS),$$(shell f='$$(f)'; echo $$$${f%->*})) + @$$(foreach f, $$($(2)_HEADERS),$$(call cp_cmd,$$(SOURCES_DIR)/$$(f),$(1)/$$(notdir $$(f))); ) +# FIXME: Use correct build dir, then document it (in the line bellow) + @$$(foreach f, $$($(2)_GEN_HEADERS),$$(call cp_cmd,$$(LOCAL_BUILD_DIR)/$$(f),$(1)/$$(notdir $$(f))); ) + @$$(foreach f, $$(nobase_$(2)_HEADERS), $$(call cp_cmd,$$(SOURCES_DIR)/$$(f),$(1)/$$(f)); ) + @$$(foreach f, $$(renamed_$(2)_HEADERS), \ + f='$$(f)'; srcfname=$$$${f%->*}; destfname=$$$${f#*->}; \ + $$(call cp_cmd,$$(SOURCES_DIR)/$$$${srcfname},$(1)/$$$${destfname}); ) + @$$(foreach f, $$(renamed_$(2)_GEN_HEADERS), \ + f='$$(f)'; srcfname=$$$${f%->*}; destfname=$$$${f#*->}; \ + $$(call cp_cmd,$$(LOCAL_BUILD_DIR)/$$$${srcfname},$(1)/$$$${destfname}); ) +# Suppress "Nothing to be done for `include-pass-local'" message if no headers are defined in Makefile.omk + @$$(if $$($(2)_HEADERS)$$($(2)_GEN_HEADERS)$$(nobase_$(2)_HEADERS)$$(renamed_$(2)_HEADERS)$$(renamed_$(2)_GEN_HEADERS),,true) +endef + #OMK:linux.omk@Makefile.rules.linux +# Hack to check RT-Linux rules +#LINUX_DIR := /home/cvs/ocera/ocera-build/kernel/linux +#RTL_DIR := /home/cvs/ocera/ocera-build/kernel/rtlinux +#CONFIG_RTLINUX = y +#OCERA_DIR := $(shell ( cd -L $(OUTPUT_DIR)/../../.. ; pwd -L ) ) + +-include $(OUTPUT_DIR)/OCERA_TOP_DIR + +BUILD_DIR_NAME = _build +COMPILED_DIR_NAME = _compiled +ifndef GROUP_DIR_NAME +GROUP_DIR_NAME = nogroup +endif + +ifdef OCERA_DIR +ifeq ($(wildcard $(OCERA_DIR)/ocera.mk),) +$(warning "ocera.mk" file does not exist. Adapt Makefile.rules for standalone compilation) +$(warning (comment out definition of OCERA_DIR line and optionally select RTL_DIR) ) +$(error or go to the ocera/ directory and do 'make' to generate the "ocera.mk" file first, please) +endif +include $(OCERA_DIR)/ocera.mk +KERN_INCLUDE_DIR := $(OCERA_KERNEL_INCLUDES_DIR) +KERN_LIB_DIR := $(OCERA_KERNEL_LIBRARIES_DIR) +KERN_MODULES_DIR := $(OCERA_MODULES_DIR) +KERN_BUILD_DIR := $(BUILD_DIR)/kern/$(GROUP_DIR_NAME) +KERN_MODPOST_DIR := $(BUILD_DIR)/kern-modpost +USER_INCLUDE_DIR := $(OCERA_USER_INCLUDES_DIR) +USER_LIB_DIR := $(OCERA_USER_LIBRARIES_DIR) +USER_UTILS_DIR := $(TARGET_DIR)/usr/bin +USER_TESTS_DIR := $(TARGET_DIR)/usr/bin +USER_BIN_DIR := $(TARGET_DIR)/usr/bin +USER_BUILD_DIR := $(BUILD_DIR)/user/$(GROUP_DIR_NAME) +#LINUX_DIR := $(OCERA_DIR)/kernel/linux +#RTL_DIR := $(OCERA_DIR)/kernel/rtlinux +#CONFIG_FILE := $(OCERA_DIR)/emdebsys/.config +ifneq ($(wildcard $(CONFIG_FILE)),) +CONFIG_FILE_OK = y +endif +else # OCERA_DIR +KERN_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include-kern +KERN_LIB_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib-kern +KERN_MODULES_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/modules +KERN_BUILD_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/kern +KERN_MODPOST_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/kern-modpost +USER_INCLUDE_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/include +USER_LIB_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/lib +USER_UTILS_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-utils +USER_TESTS_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin-tests +USER_BIN_DIR := $(OUTPUT_DIR)/$(COMPILED_DIR_NAME)/bin +USER_BUILD_DIR := $(OUTPUT_DIR)/$(BUILD_DIR_NAME)/user + +ifndef LINUX_VERSION +LINUX_VERSION=$(shell uname -r) +endif +ifndef LINUX_DIR +LINUX_DIR=/lib/modules/$(LINUX_VERSION)/build +endif +endif # OCERA_DIR + +ifeq ($(BUILD_OS),) + # Check for target + ifeq ($(OS),Windows_NT) + BUILD_OS := win32 + else + BUILD_OS := $(shell uname | tr '[A-Z]' '[a-z]' ) + #$(warning BUILD_OS=$(BUILD_OS)) + endif +endif + +ifeq ($(TARGET_OS),) + TARGET_OS := $(BUILD_OS) +endif + +export TARGET_OS +export BUILD_OS + +LOCAL_BUILD_DIR = $(USER_OBJS_DIR) + +# Assign default values to CFLAGS variable. If the variable is defined +# earlier (i.g. in config.omk), it is not overriden here. +CFLAGS ?= -O2 -Wall +CXXFLAGS ?= -O2 -Wall + + +CPPFLAGS += -I $(USER_INCLUDE_DIR) + +LOADLIBES += -L$(USER_LIB_DIR) + +LOADLIBES += $(lib_LOADLIBES:%=-l%) + +LIB_CPPFLAGS += $(CPPFLAGS) +LIB_CFLAGS += $(CFLAGS) + +ifeq ($(TARGET_OS),win32) + EXE_SUFFIX = .exe + SOLIB_EXT = dll +else + SOLIB_EXT = so + SOLIB_PICFLAGS += -fpic +endif + +#vpath %.c $(SOURCES_DIR) +#vpath %.cc $(SOURCES_DIR) +#vpath %.cxx $(SOURCES_DIR) + +USER_OBJS_DIR = $(USER_BUILD_DIR)/$(RELATIVE_DIR) +KERN_OBJS_DIR = $(KERN_BUILD_DIR)/$(RELATIVE_DIR) +OMK_WORK_DIR = $(USER_OBJS_DIR) + +.PHONY: dep subdirs clean clean-custom cleandepend check-dir + +# Some support to serialize some targets for parallel make +ifneq ($(OMK_SERIALIZE_INCLUDED),y) +include-pass: check-dir +library-pass: include-pass +link-pseudo-pass: library-pass +binary-pass: link-pseudo-pass +kernel-lib-pass: include-pass +kernel-mod-pass: kernel-lib-pass +kernel-modpost-pass: kernel-mod-pass +kernel-pass: kernel-mod-pass kernel-modpost-pass + +override OMK_SERIALIZE_INCLUDED = y +MAKEOVERRIDES := $(filter-out OMK_SERIALIZE_INCLUDED=n,$(MAKEOVERRIDES)) +endif + +#===================================================================== +# User-space rules and templates to compile programs, libraries etc. + +ifdef USER_RULE_TEMPLATES + +USER_SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.S .o/.o + +USER_SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.S .lo/.lo + +#%.lo: %.c +# $(CC) -o $@ $(LCFLAGS) -c $< + +c_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -DOMK_FOR_USER + +cc_o_COMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -DOMK_FOR_USER + +S_o_COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(ASFLAGS) -DOMK_FOR_USER + +idl_COMPILE = $(IDL_COMPILER) + +# Check GCC version for user build +ifndef CC_MAJOR_VERSION +CC_MAJOR_VERSION := $(shell $(CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/') +endif +# Prepare suitable define for dependency building +ifeq ($(CC_MAJOR_VERSION),2) +CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp" +else +CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp" +endif + + +# Syntax: $(call COMPILE_c_o_template,,,) +define COMPILE_c_o_template +$(2): $(1) $$(GEN_HEADERS) + @$(QUIET_CMD_ECHO) " CC $$@" + $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + +# Syntax: $(call COMPILE_cc_o_template,,,) +define COMPILE_cc_o_template +$(2): $(1) $$(GEN_HEADERS) + @$(QUIET_CMD_ECHO) " CXX $$@" + $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + +# Syntax: $(call COMPILE_S_o_template,,,) +define COMPILE_S_o_template +$(2): $(1) $$(GEN_HEADERS) + @$(QUIET_CMD_ECHO) " AS $$@" + $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + +NM ?= nm + +# Syntax: $(call CMETRIC_o_h_template,,) +define CMETRIC_o_h_template +$(2): $(1) + @$(QUIET_CMD_ECHO) " CMETRIC $$@" + $(Q)if [ -n `dirname $$@` ] ; then \ + if [ ! -e `dirname $$@` ] ; then \ + mkdir -p `dirname $$@` ; fi ; fi + $(Q)echo >$$@.tmp '/* Automatically generated from $$< */' + $(Q)echo >>$$@.tmp '/* Conditionals to control compilation */' +# Bellow, the tricks with redirection are for shells without set -o pipefail +# (see http://www.mail-archive.com/dash@vger.kernel.org/msg00149.html) + $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\ + | $(SED4OMK) -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2cond_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \ + | sort >>$$@.tmp` && exit $$$$status + $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in hexadecimal format */' + $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) $$<; echo $$$$? >&4; }\ + | $(SED4OMK) -n 's/^ *0*\(0[0-9A-Fa-f]*\) *A *_cmetric2def_\([A-Za-z_0-9]*\) */#define \2 0x\1/p' \ + | sort >>$$@.tmp` && exit $$$$status + $(Q)echo >>$$@.tmp '/* Defines from the values defined to symbols in decimal format */' + $(Q)exec 3>&1; status=`exec 4>&1 >&3; { $(NM) -td $$<; echo $$$$? >&4; }\ + | $(SED4OMK) -n 's/^ *0*\(0\|[1-9][0-9]*\) *A *_cmetric2defdec_\([A-Za-z_0-9]*\) */#define \2 \1/p' \ + | sort >>$$@.tmp` && exit $$$$status + $(Q)mv $$@.tmp $$@ +endef + + + +define COMPILE_idl_template +$(2).c $(2)-stubs.c $(2)-skels.c $(2)-common.c $(2).h: $(1) $$(wildcard $$(firstword $$(idl_COMPILE))) + @$(QUIET_CMD_ECHO) " IDL $$@" + $(Q) $$(idl_COMPILE) $$($(2)_IDLFLAGS) $(1) +endef + + +# Syntax: $(call PROGRAM_template,,,,) +# FIXME: ???????? asi je tu blbej komentar +define PROGRAM_template + +USER_IDLS += $$($(1)_SERVER_IDL) $$($(1)_CLIENT_IDL) $$($(1)_IDL) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-skels.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_IDL:%.idl=%.c)) +USER_GEN_SOURCES += $$($(1)_GEN_SOURCES) + +$(foreach x, $(USER_SOURCES2OBJS), +$(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ + $$($(1)_SOURCES) $$($(1)_GEN_SOURCES))) +) +$(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%)) + +USER_OBJS += $$($(1)_OBJS) +USER_SOURCES += $$($(1)_SOURCES) + +$(2)/$(1)$(3): $$($(1)_OBJS) + @$(QUIET_CMD_ECHO) " LINK $$@" + $(Q) $$(if $$(filter %.cc,$$($(1)_SOURCES:%.cxx=%.cc)),$$(CXX),$$(CC)) \ + $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) $$(LOADLIBES) $$(LDFLAGS) $$($(1)_LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$@ + @echo "$(2)/$(1)$(3): \\" >$(USER_OBJS_DIR)/$(1).exe.d + @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$| \1 \&|p' $(USER_OBJS_DIR)/$(1).exe.map|tr '&' '\134' >>$(USER_OBJS_DIR)/$(1).exe.d + @echo >>$(USER_OBJS_DIR)/$(1).exe.d +endef + +# Usage: $(call SCRIPT_template,,) +define SCRIPT_template +$(2)/$(1): $$(SOURCES_DIR)/$(1) + @$(QUIET_CMD_ECHO) " CP $$@" + $(Q)cp $$^ $$@ +endef + + +# Syntax: $(call LIBRARY_template,) +define LIBRARY_template + +USER_IDLS += $$($(1)_SERVER_IDL) $$($(1)_CLIENT_IDL) $$($(1)_IDL) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-skels.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_IDL:%.idl=%.c)) +USER_GEN_SOURCES += $$($(1)_GEN_SOURCES) + +$(foreach x, $(USER_SOURCES2OBJS), +$(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ + $$($(1)_SOURCES) $$($(1)_GEN_SOURCES))) +) +$(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%)) + +USER_OBJS += $$($(1)_OBJS) +USER_SOURCES += $$($(1)_SOURCES) + +$(USER_LIB_DIR)/lib$(1).a: $$($(1)_OBJS) + @$(QUIET_CMD_ECHO) " AR $$@" + $(Q) $(AR) rcs $$@ $$^ +endef + +.PHONY: FORCE + +# Syntax: $(call SOLIB_template,) +define SOLIB_template + +USER_IDLS += $$($(1)_SERVER_IDL) $$($(1)_CLIENT_IDL) $$($(1)_IDL) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-skels.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-common.c)) +$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_IDL:%.idl=%.c)) +SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES) + +$(foreach x, $(USER_SOURCES2OBJSLO), +$(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\ + $$($(1)_SOURCES) $$($(1)_GEN_SOURCES))) +) +$(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%)) + +SOLIB_OBJS += $$($(1)_OBJSLO) +SOLIB_SOURCES += $$($(1)_SOURCES) + +$(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: $$($(1)_OBJSLO) FORCE + $(Q)echo '$(1)_objslo += $$$$(addprefix $(USER_OBJS_DIR)/,$$($(1)_OBJSLO))' > $$@.tmp; \ + echo '$(1)_libs += $$($(1)_LIBS) $$(lib_LOADLIBES)' >> $$@.tmp; \ + echo '$(1)_ldflags += $$($(1)_LDFLAGS) $$(lib_LDFLAGS)' >> $$@.tmp; \ + echo 'shared_libs := $$$$(sort $(1) $$$$(shared_libs))' >> $$@.tmp; \ + if cmp -s $$@.tmp $$@; then rm $$@.tmp; else mv $$@.tmp $$@; fi +endef + +library-pass-local: $(addprefix $(USER_INCLUDE_DIR)/,$(cmetric_include_HEADERS)) \ + $(lib_LIBRARIES:%=$(USER_LIB_DIR)/lib%.a) $(shared_LIBRARIES:%=$(OMK_WORK_DIR)/lib%.$(SOLIB_EXT).omkvar) + +binary-pass-local: $(bin_PROGRAMS:%=$(USER_BIN_DIR)/%$(EXE_SUFFIX)) \ + $(utils_PROGRAMS:%=$(USER_UTILS_DIR)/%$(EXE_SUFFIX)) \ + $(test_PROGRAMS:%=$(USER_TESTS_DIR)/%$(EXE_SUFFIX)) \ + $(bin_SCRIPTS:%=$(USER_BIN_DIR)/%) + +# Special rules for CMETRIC generated headers + +$(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call COMPILE_c_o_template,\ + $(SOURCES_DIR)/$($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES),\ + $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),))) +$(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call CMETRIC_o_h_template,\ + $($(basename $(notdir $(cmetrh)))_CMETRIC_SOURCES:%.c=%.o),\ + $(addprefix $(USER_INCLUDE_DIR)/,$(cmetrh))))) + +GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%) + +GEN_HEADERS+=$(filter %.h,$(USER_IDLS:%.idl=%.h)) + +# Generate rules for compilation of programs and libraries + +$(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX)))) + +$(foreach prog,$(test_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_TESTS_DIR),$(EXE_SUFFIX)))) + +$(foreach prog,$(bin_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_BIN_DIR),$(EXE_SUFFIX)))) + +$(foreach script,$(bin_SCRIPTS),$(eval $(call SCRIPT_template,$(script),$(USER_BIN_DIR)))) + + +$(foreach lib,$(lib_LIBRARIES),$(eval $(call LIBRARY_template,$(lib)))) + +$(foreach lib,$(shared_LIBRARIES),$(eval $(call SOLIB_template,$(lib)))) + +-include $(USER_OBJS_DIR)/*.d +#FIXME: This doesn't include dependencies of source files from +#subdirectories (i.s. *_SOURCES=dir/file.c. I'm currently not sure, +#how to handle this. + +endif # USER_RULE_TEMPLATES + +.PHONY: link-pseudo-pass +link-pseudo-pass: + $(Q)$(MAKE) $(NO_PRINT_DIRECTORY) -C $(USER_BUILD_DIR) -f $(SOURCESDIR_MAKEFILE) link-shared-libs + +ifeq ($(MAKECMDGOALS),link-shared-libs) + +# Syntax: $(call solib_link_template,) +define solib_link_template +$(1)_shared_libs = $$(patsubst %,$(USER_LIB_DIR)/lib%.$(SOLIB_EXT),$$(filter $$(shared_libs),$$($(1)_libs))) +#$$(warning $(1)_shared_libs = $$($(1)_shared_libs)) +$(USER_LIB_DIR)/lib$(1).$(SOLIB_EXT): $$($(1)_shared_libs) $$($(1)_objslo) + @$(QUIET_CMD_ECHO) " LINK $$@" + $(Q)$(CC) --shared -Xlinker -soname=lib$(1).$(SOLIB_EXT) -Wl,-Map,$(USER_OBJS_DIR)/lib$(1).$(SOLIB_EXT).map -o $$@ $$($(1)_objslo) $$(LOADLIBES) $$($(1)_libs:%=-l%) $$(lib_ldflags) $$($(1)_ldflags) +endef + +-include $(shell true; find $(USER_BUILD_DIR) -name 'lib*.omkvar') # `true' is a hack for MinGW +#$(warning $(shared_libs)) +$(foreach lib,$(shared_libs),$(eval $(call solib_link_template,$(lib)))) + +.PHONY: link-shared-libs +link-shared-libs: $(shared_libs:%=$(USER_LIB_DIR)/lib%.$(SOLIB_EXT)) +endif # link-shared-libs + +#===================================================================== +# Kernel-space rules and templates to compile modules, libraries etc. + +ifdef KERN_RULE_TEMPLATES + +$(KERN_LIB_DIR)/kernel.mk: $(LINUX_DIR)/.config $(MAKERULES_DIR)/kernelcfg2mk + @$(QUIET_CMD_ECHO) " KCFG2MK $@" + $(Q) $(MAKERULES_DIR)/kernelcfg2mk $(LINUX_DIR) $(KERN_LIB_DIR) + +ifeq ($(CONFIG_RTLINUX),y) +include $(RTL_DIR)/rtl.mk + +KERN_CC = $(CC) +kern_GCCLIB_DIR=$(shell LANG=C LC_ALL=C LC_MESSAGES=C $(CC) -print-search-dirs | $(SED4OMK) -n -e 's/^install: \(.*\)$$/\1/p' ) +INCLUDES := -I $(KERN_INCLUDE_DIR) $(INCLUDE) $(rtlinux_INCLUDES) $(kernel_INCLUDES) +#-DEXPORT_NO_SYMBOLS +c_o_kern_COMPILE = $(KERN_CC) -idirafter $(kern_GCCLIB_DIR)/include $(INCLUDES) $(CFLAGS) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB -nostdinc +cc_o_kern_COMPILE = $(CXX) $(INCLUDES) $(CXXFLAGS) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB +KERN_EXE_SUFFIX := .o +KERN_ARCH = $(ARCH) +KERN_LD = $(LD) +KERN_AR = $(AR) + +else # CONFIG_RTLINUX + +include $(KERN_LIB_DIR)/kernel.mk + +ifeq ($(LINUX_SRC),) +LINUX_SRC = $(LINUX_DIR) +endif +kernel_INCLUDES += -I $(KERN_INCLUDE_DIR) -I $(LINUX_DIR) -idirafter $(LINUX_SRC)/include/linux + +ifdef LINUX_CC +KERN_CC = $(LINUX_CC) +kern_GCCLIB_DIR=$(shell LANG=C LC_ALL=C LC_MESSAGES=C $(LINUX_CC) -print-search-dirs | $(SED4OMK) -n -e 's/^install: \(.*\)$$/\1/p' ) +else +KERN_CC = echo KERN_CC not defined - compilation skipped +endif +c_o_kern_COMPILE = $(KERN_CC) $(kernel_INCLUDES) -idirafter $(kern_GCCLIB_DIR)/include $(LINUX_CPPFLAGS) $(LINUX_CFLAGS) $(LINUX_CFLAGS_MODULE) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB -nostdinc +cc_o_kern_COMPILE = $(KERN_CC) $(kernel_INCLUDES) -idirafter $(kern_GCCLIB_DIR)/include $(LINUX_CPPFLAGS) $(LINUX_CFLAGS) $(LINUX_CFLAGS_MODULE) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB +S_o_kern_COMPILE = $(KERN_CC) $(kernel_INCLUDES) -idirafter $(kern_GCCLIB_DIR)/include $(LINUX_CPPFLAGS) $(LINUX_AFLAGS) $(LINUX_AFLAGS_MODULE) -DOMK_FOR_KERNEL -DEXPORT_SYMTAB -nostdinc +KERN_EXE_SUFFIX := $(LINUX_MODULE_EXT) +KERN_LDFLAGS = $(LINUX_LDFLAGS) $(LINUX_LDFLAGS_MODULE) +ifdef LINUX_ARCH +KERN_ARCH = $(LINUX_ARCH) +else +KERN_ARCH = echo KERN_ARCH not defined - skipped +endif +ifdef LINUX_LD +KERN_LD = $(LINUX_LD) +else +KERN_LD = echo KERN_LD not defined - skipped +endif +ifneq ($(LINUX_AR),) +KERN_AR = $(LINUX_AR) +else +KERN_AR = $(AR) +endif +ifeq ($(LINUX_QUOTE_MODNAME),y) +KERN_MQ=\" +KERN_KBUILD_MODNAME=-D"KBUILD_MODNAME=((THIS_MODULE)!=NULL?(THIS_MODULE)->name:NULL)" +endif +endif # CONFIG_RTLINUX + +KERN_LOADLIBES += -L$(KERN_LIB_DIR) + +KERN_LOADLIBES += $(rtlinux_LOADLIBES:%=-l%) +KERN_LOADLIBES += $(kernel_LOADLIBES:%=-l%) + + + +# Check GCC version for kernel part of build +ifndef kern_CC_MAJOR_VERSION +kern_CC_MAJOR_VERSION := $(shell $(KERN_CC) -dumpversion | $(SED4OMK) -e 's/\([^.]\)\..*/\1/') +endif +# Prepare suitable define for dependency building +ifeq ($(kern_CC_MAJOR_VERSION),2) +kern_CC_DEPFLAGS = -Wp,-MD,"$@.d.tmp" +else +kern_CC_DEPFLAGS = -MT $@ -MD -MP -MF "$@.d.tmp" +endif + +ifeq ($(KERN_EXE_SUFFIX),.ko) +ifeq ($(wildcard $(LINUX_DIR)/scripts/mod/modpost),) +KERN_MODPOST = $(LINUX_DIR)/scripts/modpost +else +KERN_MODPOST = $(LINUX_DIR)/scripts/mod/modpost +endif +KERN_MODULES_LINK_DIR = $(KERN_MODPOST_DIR) +KERN_LINK_SUFFIX = .o +else +KERN_MODULES_LINK_DIR = $(KERN_MODULES_DIR) +KERN_LINK_SUFFIX = $(KERN_EXE_SUFFIX) +endif + +ifeq ($(LINUX_CONFIG_MODVERSIONS),y) +MODPOST_OPTS += -m +MODPOST_OPTS += -i $(LINUX_DIR)/Module.symvers +ifneq ($(LINUX_BUILDHOST),) # this is not correct point, it should look for 2.6.17 kernel +MODPOST_OPTS += -I $(KERN_LIB_DIR)/Module.symvers +endif +MODPOST_OPTS += -o $(KERN_LIB_DIR)/Module.symvers +endif + +ifeq ($(LINUX_CONFIG_DEBUG_SECTION_MISMATCH),y) +MODPOST_OPTS += -S +endif + +ifeq ($(LINUX_CONFIG_MARKERS),y) +MODPOST_OPTS += -K $(LINUX_DIR)/Module.markers +MODPOST_OPTS += -M $(KERN_LIB_DIR)/Module.markers +endif + +ifeq ($(LINUX_KBUILD_MODPOST_WARN),y) +MODPOST_OPTS += -w +endif + +ifneq ($(LINUX_BUILDHOST),) +ifneq ($(LINUX_BUILDHOST),$(LINUX_ARCH)) +MODPOST_OPTS += -c +endif +endif + +define COMPILE_c_o_kern_template + +$(2): $(1) + @$(QUIET_CMD_ECHO) " CC [K] $$@" + $(Q) if $$(c_o_kern_COMPILE) $$(kern_CC_DEPFLAGS) $(3) $(KERN_KBUILD_MODNAME) \ + -D"KBUILD_BASENAME=$(KERN_MQ)$(notdir $(basename $(1)))$(KERN_MQ)" \ + -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + + +define COMPILE_cc_o_kern_template + +$(2): $(1) + @$(QUIET_CMD_ECHO) " CXX [K] $$@" + $(Q) if $$(cc_o_kern_COMPILE) $$(kern_CC_DEPFLAGS) $(3) $(KERN_KBUILD_MODNAME) \ + -D"KBUILD_BASENAME=$(KERN_MQ)$(notdir $(basename $(1)))$(KERN_MQ)" \ + -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + + +define COMPILE_S_o_kern_template + +$(2): $(1) + @$(QUIET_CMD_ECHO) " AS [K] $$@" + $(Q) if $$(S_o_kern_COMPILE) $$(kern_CC_DEPFLAGS) $(3) $(KERN_KBUILD_MODNAME) \ + -D"KBUILD_BASENAME=$(KERN_MQ)$(notdir $(basename $(1)))$(KERN_MQ)" \ + -o $$@ -c $$< ; \ + then mv -f "$$@.d.tmp" "$$@.d" ; \ + else rm -f "$$@.d.tmp" ; exit 1; \ + fi +endef + + + +define MODULE_kern_template +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o)) +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o)) +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o)) +$(1)_OBJS := $$(sort $$($(1)_OBJS)) + +KERN_OBJS += $$($(1)_OBJS) +KERN_SOURCES += $$($(1)_SOURCES) + +# this is hack to build "__this_module" structure for 2.6.x kernels +# modpost is used for that purpose now + +#$(1).mod.c: +# echo "\ +# #include @\ +# #include @\ +# #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,5))@\ +# #include @\ +# #include @\ +# @\ +# MODULE_INFO(vermagic, VERMAGIC_STRING);@\ +# @\ +# #undef unix@\ +# struct module __this_module@\ +# __attribute__((section(\".gnu.linkonce.this_module\"))) = {@\ +# .name = __stringify(KBUILD_MODNAME),@\ +# .init = init_module,@\ +# #ifdef CONFIG_MODULE_UNLOAD@\ +# .exit = cleanup_module,@\ +# #endif@\ +# };@\ +# #endif@\ +# " | tr @ \\n >$$@ + + +#$(eval $(call COMPILE_c_o_kern_template,$(1).mod.c,$(1).mod.o,-DKBUILD_MODNAME=$(1))) + +$(2)/$(1)$(KERN_LINK_SUFFIX): $$($(1)_OBJS) + @$(QUIET_CMD_ECHO) " LD [K] $$@" + $(Q) $$(KERN_LD) $$(KERN_LDFLAGS) -r $$($(1)_OBJS) -L$$(kern_GCCLIB_DIR) $$($(1)_LIBS:%=-l%) $$(KERN_LOADLIBES) -Map $(KERN_OBJS_DIR)/$(1).mod.map -o $$@ + @echo "$(2)/$(1)$(KERN_LINK_SUFFIX): \\" >$(KERN_OBJS_DIR)/$(1).mod.d + @$(SED4OMK) -n -e 's/^LOAD \(.*\)$$$$/ \1 \\/p' $(KERN_OBJS_DIR)/$(1).mod.map >>$(KERN_OBJS_DIR)/$(1).mod.d + @echo >>$(KERN_OBJS_DIR)/$(1).mod.d + @if [ "$(KERN_EXE_SUFFIX)" = ".ko" ] ; then \ + echo $(1) >>$(KERN_MODPOST_DIR)/module-changes ; \ + echo $(1) >$(KERN_MODPOST_DIR)/$(1).mod.stamp ; \ + fi + +endef + + + +define LIBRARY_kern_template +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.c=%.o)) +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cc=%.o)) +$(1)_OBJS += $$(filter %.o,$$($(1)_SOURCES:%.cxx=%.o)) +$(1)_OBJS := $$(sort $$($(1)_OBJS)) + +KERN_OBJS += $$($(1)_OBJS) +KERN_SOURCES += $$($(1)_SOURCES) + +$(KERN_LIB_DIR)/lib$(1).a: $$($(1)_OBJS) + @$(QUIET_CMD_ECHO) " AR [K] $$@" + $(Q) $(KERN_AR) rcs $$@ $$^ +endef + + +ifdef LINUX_DIR + +kernel-lib-pass-local: $(kernel_LIBRARIES:%=$(KERN_LIB_DIR)/lib%.a) + +kernel-mod-pass-local: $(kernel_MODULES:%=$(KERN_MODULES_LINK_DIR)/%$(KERN_LINK_SUFFIX)) + +$(foreach module,$(kernel_MODULES),$(eval $(call MODULE_kern_template,$(module),$(KERN_MODULES_LINK_DIR)))) + +$(foreach lib,$(kernel_LIBRARIES),$(eval $(call LIBRARY_kern_template,$(lib)))) + +endif + + +ifeq ($(CONFIG_RTLINUX),y) + +kernel-mod-pass-local: $(rtlinux_MODULES:%=$(KERN_MODULES_LINK_DIR)/%$(KERN_LINK_SUFFIX)) + +kernel-lib-pass-local: $(rtlinux_LIBRARIES:%=$(KERN_LIB_DIR)/lib%.a) + +$(foreach module,$(rtlinux_MODULES),$(eval $(call MODULE_kern_template,$(module),$(KERN_MODULES_LINK_DIR)))) + +$(foreach lib,$(rtlinux_LIBRARIES),$(eval $(call LIBRARY_kern_template,$(lib)))) + +endif + +ifeq ($(KERN_MODPOST_PASS),y) + +MODULES_LIST := $(wildcard *.mod.stamp) +MODULES_LIST := $(MODULES_LIST:%.mod.stamp=%) + +define MODPOST_kern_template +$(2) : $(1)$(KERN_LINK_SUFFIX) $(1).mod.c + @$(QUIET_CMD_ECHO) " LD [M] $$@" + $(Q) $$(c_o_kern_COMPILE) -D"KBUILD_BASENAME=$(KERN_MQ)$(1)$(KERN_MQ)" \ + -D"KBUILD_MODNAME=$(KERN_MQ)$(1)$(KERN_MQ)" \ + -o $(1).mod.o -c $(1).mod.c + $(Q) $$(KERN_LD) $$(KERN_LDFLAGS) $(1)$(KERN_LINK_SUFFIX) $(1).mod.o -r -o $$@ +endef + +kernel-modpost-versions: $(wildcard $(LINUX_DIR)/Module.symvers) + @$(QUIET_CMD_ECHO) " MODPOST $(KERN_MODPOST_DIR)" + @echo >$(KERN_MODPOST_DIR)/modpost-running + @rm -f $(KERN_MODPOST_DIR)/module-changes + $(Q) $(KERN_MODPOST) $(MODPOST_OPTS) $(MODULES_LIST:%=%$(KERN_LINK_SUFFIX)) + +$(MODULES_LIST:%=%.mod.c) : kernel-modpost-versions + +kernel-modpost-pass-local: $(MODULES_LIST:%=$(KERN_MODULES_DIR)/%$(KERN_EXE_SUFFIX)) + @rm -f $(KERN_MODPOST_DIR)/modpost-running + +$(foreach module,$(MODULES_LIST),$(eval $(call MODPOST_kern_template,$(module),$(module:%=$(KERN_MODULES_DIR)/%$(KERN_EXE_SUFFIX))))) + +endif + +-include $(KERN_OBJS_DIR)/*.d + +endif + +#===================================================================== + +# Kernel requires its own set of configuration header-files +ifneq ($(kernel_LIBRARIES)$(rtlinux_LIBRARIES)$(kernel_MODULES)$(rtlinux_MODULES)$(kernel_HEADERS)$(rtlinux_HEADERS)$(kernel_HEADERS)$(rtlinux_HEADERS)$(nobase_kernel_HEADERS)$(nobase_rtlinux_HEADERS)$(renamed_kernel_HEADERS)$(renamed_rtlinux_HEADERS),) +KERN_CONFIG_HEADERS_REQUIRED = y +endif + +$(eval $(call omk_pass_template, kernel-lib-pass,$(KERN_OBJS_DIR),KERN_RULE_TEMPLATES=y,$(kernel_LIBRARIES)$(rtlinux_LIBRARIES))) +$(eval $(call omk_pass_template, kernel-mod-pass,$(KERN_OBJS_DIR),KERN_RULE_TEMPLATES=y,$(kernel_MODULES)$(rtlinux_MODULES))) + +kernel-modpost-pass: + +@if [ -e "$(KERN_MODPOST_DIR)/module-changes" -o -e "$(KERN_MODPOST_DIR)/modpost-running" ] ; \ + then \ + $(MAKE) --no-print-directory -C $(KERN_MODPOST_DIR) \ + -f $(SOURCES_DIR)/Makefile KERN_RULE_TEMPLATES=y KERN_MODPOST_PASS=y $(@:%=%-local) ; \ + fi + +$(eval $(call omk_pass_template, library-pass,$(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(lib_LIBRARIES)$(shared_LIBRARIES))) +$(eval $(call omk_pass_template, binary-pass, $(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,$(bin_PROGRAMS)$(utils_PROGRAMS)$(test_PROGRAMS)$(bin_SCRIPTS))) + +$(eval $(call omk_pass_template,clean,$(USER_OBJS_DIR),,always)) +$(eval $(call omk_pass_template,install,$(USER_OBJS_DIR),,always)) +$(eval $(call omk_pass_template,include-pass,$(USER_OBJS_DIR),USER_RULE_TEMPLATES=y,always)) + +check-dir:: + @$(call mkdir_def,$(USER_BUILD_DIR)) + @$(call mkdir_def,$(KERN_BUILD_DIR)) + @$(call mkdir_def,$(USER_INCLUDE_DIR)) + @$(call mkdir_def,$(KERN_INCLUDE_DIR)) + @$(call mkdir_def,$(USER_LIB_DIR)) + @$(call mkdir_def,$(KERN_LIB_DIR)) + @$(call mkdir_def,$(USER_BIN_DIR)) + @$(call mkdir_def,$(USER_UTILS_DIR)) + @$(call mkdir_def,$(USER_TESTS_DIR)) + @$(call mkdir_def,$(KERN_MODULES_DIR)) + @$(call mkdir_def,$(KERN_MODPOST_DIR)) + +install-local: # TODO + +$(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include)) +$(eval $(call include-pass-template,$(KERN_INCLUDE_DIR),kernel)) +ifeq ($(CONFIG_RTLINUX),y) +$(eval $(call include-pass-template,$(KERN_INCLUDE_DIR),rtlinux)) +endif + + +ifdef USER_RULE_TEMPLATES + +# User-space static libraries and applications object files + +USER_SOURCES := $(sort $(USER_SOURCES)) + +USER_GEN_SOURCES := $(sort $(USER_GEN_SOURCES)) + +#$(warning USER_SOURCES = $(USER_SOURCES)) + +$(foreach src,$(filter %.c,$(USER_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),))) + +$(foreach src,$(filter %.cc,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),))) + +$(foreach src,$(filter %.cxx,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),))) + +$(foreach src,$(filter %.S,$(USER_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.o),))) + +$(foreach src,$(filter %.c,$(USER_GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.o),))) + +# User-space shared libraries object files + +SOLIB_SOURCES := $(sort $(SOLIB_SOURCES)) + +SOLIB_GEN_SOURCES := $(sort $(SOLIB_GEN_SOURCES)) + +#$(warning SOLIB_SOURCES = $(SOLIB_SOURCES)) +#$(warning SOLIB_GEN_SOURCES = $(SOLIB_GEN_SOURCES)) + +$(foreach src,$(filter %.c,$(SOLIB_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS)))) + +$(foreach src,$(filter %.cc,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.lo),$(SOLIB_PICFLAGS)))) + +$(foreach src,$(filter %.cxx,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.lo),$(SOLIB_PICFLAGS)))) + +$(foreach src,$(filter %.S,$(SOLIB_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.lo),$(SOLIB_PICFLAGS)))) + +$(foreach src,$(filter %.c,$(SOLIB_GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS)))) + +# Create _build directories for sources in subdirectories i.e. *_SOURCES=dir/file.c +_dirs_to_create=$(filter-out ./,$(sort $(dir $(USER_SOURCES) $(SOLIB_SOURCES)))) +ifneq ($(_dirs_to_create),) +$(shell mkdir -p $(addprefix $(LOCAL_BUILD_DIR)/,$(_dirs_to_create))) +endif + +# IDL compilation + +USER_IDLS := $(sort $(USER_IDLS)) + +$(foreach src,$(filter %.idl,$(USER_IDLS)),$(eval $(call COMPILE_idl_template,$(SOURCES_DIR)/$(src),$(src:%.idl=%)))) + +endif + +ifdef KERN_RULE_TEMPLATES + +KERN_SOURCES := $(sort $(KERN_SOURCES)) + +#$(warning KERN_SOURCES = $(KERN_SOURCES)) + +$(foreach src,$(filter %.c,$(KERN_SOURCES)),$(eval $(call COMPILE_c_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),))) + +$(foreach src,$(filter %.cc,$(KERN_SOURCES)),$(eval $(call COMPILE_cc_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),))) + +$(foreach src,$(filter %.cxx,$(KERN_SOURCES)),$(eval $(call COMPILE_cc_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),))) + +$(foreach src,$(filter %.S,$(USER_SOURCES)),$(eval $(call COMPILE_S_o_kern_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.o),))) +endif + +clean-local: clean-custom + @echo Cleaning in $(KERN_OBJS_DIR) and $(USER_OBJS_DIR) + @rm -f $(KERN_OBJS_DIR)/*.o $(USER_OBJS_DIR)/*.[och] $(USER_OBJS_DIR)/*.lo\ + $(KERN_OBJS_DIR)/*.d $(USER_OBJS_DIR)/*.d \ + $(KERN_OBJS_DIR)/*.map $(USER_OBJS_DIR)/*.map \ + $(KERN_OBJS_DIR)/*.mod.c \ + $(kernel_MODULES:%=$(KERN_MODPOST_DIR)/%.*) \ + $(LOCAL_CONFIG_H:%=$(KERN_OBJS_DIR)/%) \ + $(LOCAL_CONFIG_H:%=$(USER_OBJS_DIR)/%) + @if [ -e $(KERN_LIB_DIR)/kernel.mk ] ; then \ + touch -t 200001010101 $(KERN_LIB_DIR)/kernel.mk ; \ + fi + +include-pass-submakes: extra-rules-subdirs + +# We must go to EXTRA_RULES_SUBDIRS before going to any other +# directory, since the executables compiled in EXTRA_RULES_SUBDIRS +# might be needed there. +include-pass-this-dir $(foreach subdir,$(SUBDIRS),include-pass-$(subdir)-subdir): extra-rules-subdirs + +default: include-pass library-pass binary-pass +ifndef OMIT_KERNEL_PASSES +# Also make kernel passes if not disabled +default: kernel-lib-pass kernel-pass +endif + #OMK:config_h.omk@Makefile.rules.linux +# Syntax: $(call BUILD_CONFIG_H_template,,,,) +define BUILD_CONFIG_H_template + +$(addprefix $(1)/,$(notdir $(addsuffix .stamp,$(2)))) : $(CONFIG_FILES) + @$(QUIET_CMD_ECHO) " CONFGEN $(notdir $(2))" + @if [ ! -d `dirname $(2).tmp` ] ; then \ + mkdir -p `dirname $(2).tmp` ; fi + @echo "/* Automatically generated from */" > "$(2).tmp" + @echo "/* config files: $$(^:$(OUTPUT_DIR)/%=%) */" >> "$(2).tmp" + $(if $(DOXYGEN),@echo "/** @file */" >> "$(2).tmp") + @echo "#ifndef $(4)" >> "$(2).tmp" + @echo "#define $(4)" >> "$(2).tmp" + @( $(foreach x, $(shell echo '$($(3))' | tr 'x\t ' 'x\n\n' | $(SED4OMK) -e 's/^\([^ =]*\)\(=[^ ]\+\|\)$$/\1/' ), \ + echo '$(x).$($(x))' ; ) echo ; ) | \ + $(SED4OMK) -e '/^[^.]*\.n$$$$/d' -e '/^[^.]*\.$$$$/d' -e 's/^\([^.]*\)\.[ym]$$$$/\1.1/' | \ + $(SED4OMK) -n -e 's/^\([^.]*\)\.\(.*\)$$$$/#define \1 \2/p' \ + >> "$(2).tmp" + @echo "#endif /*$(4)*/" >> "$(2).tmp" + @touch "$$@" + @if cmp --quiet "$(2).tmp" "$(2)" ; then rm "$(2).tmp"; \ + else mv "$(2).tmp" "$(2)" ; \ + echo "Updated configuration $(2)" ; fi + +endef + +ifdef LOCAL_CONFIG_H + +# This must be declared after the default cflags are assigned! +# Override is used to override command line assignemnt. +override CFLAGS += -I $(USER_OBJS_DIR) +override kernel_INCLUDES += -I $(KERN_OBJS_DIR) +$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(USER_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) ) + +endif + +# Special rules for configuration exported headers + +#FIXME: The directory for headers should not be specified here. +$(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(USER_OBJS_DIR),$(addprefix $(USER_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\ +_$(basename $(notdir $(confh)))_H \ +))) + +config_h_stamp_files = $(addprefix $(USER_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H)))) + +# Add some hooks to standard passes +include-pass-local: $(config_h_stamp_files) + +ifneq ($(KERN_CONFIG_HEADERS_REQUIRED),) + +ifdef LOCAL_CONFIG_H +$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(KERN_OBJS_DIR)/$(LOCAL_CONFIG_H),default_CONFIG,_LOCAL_CONFIG_H) ) +endif + +$(foreach confh,$(config_include_HEADERS),$(eval $(call BUILD_CONFIG_H_template,$(KERN_OBJS_DIR),$(addprefix $(KERN_INCLUDE_DIR)/,$(confh)),$(basename $(notdir $(confh)))_DEFINES,\ +_$(basename $(notdir $(confh)))_H \ +))) + +kern_config_h_stamp_files = $(addprefix $(KERN_OBJS_DIR)/,$(notdir $(addsuffix .stamp,$(config_include_HEADERS) $(LOCAL_CONFIG_H)))) + +# Add some hooks to standard passes +include-pass-local: $(kern_config_h_stamp_files) + +endif + +clean-local: clean-local-config-h + +clean-local-config-h: + @$(foreach confh,$(config_h_stamp_files) $(kern_config_h_stamp_files),\ + if [ -e $(confh) ] ; then rm $(confh) ; fi ; \ + ) + #OMK:qt.omk@Makefile.rules.linux +ifneq ($(QT_SUBDIRS)$(QT_PROJECTS),) + +# Usage: $(call qt_project_template,<.pro_file relative to SOURCES_DIR>) +define qt_project_template + +.PHONY: qt-subpass-$(1) clean-qt-$(dir $(1)) + +# FIXME: Handle multiple .pro files correctly +$(LOCAL_BUILD_DIR)/$(dir $(1))Makefile: $(SOURCES_DIR)/$(1) + $(Q)mkdir -p $$(dir $$(@)) && cd $$(dir $$(@)) && \ + $(if $(QMAKE),$(QMAKE),$(QTDIR:%=%/bin/)qmake) \ + TOP_DIR=$(OUTPUT_DIR) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir $(1)) \ + $(QTDIR:%=QTDIR=%) CC=$(CC) CXX=$(CXX) \ + LIBS+="-L$(USER_LIB_DIR)" DESTDIR=$(USER_BIN_DIR) \ + INCLUDEPATH+="$(USER_INCLUDE_DIR)" \ + DEPENDPATH+="$(USER_INCLUDE_DIR)" \ + QMAKE_LFLAGS="-Wl,-rpath-link,$(USER_LIB_DIR) $$(QMAKE_LFLAGS)" \ + $(SOURCES_DIR)/$(1) + +# This horrible substitution is here to properly escape +# -Wl,-rpath,$ORIGIN flags. It includes escaping for make, shell, +# qmake and again make and shell run on qmake generated makefile. +QMAKE_LFLAGS = $$(subst $$$$,\\\\\\$$$$\$$$$,$$(LDFLAGS)) + +# Hook to binary pass +binary-pass-submakes: qt-subpass-$(1) +qt-subpass-$(1): $(LOCAL_BUILD_DIR)/$(dir $(1))Makefile + $(Q)$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir $(1)) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir $(1)) -C $(LOCAL_BUILD_DIR)/$(dir $(1)) || exit 1 ; + +# Hook to clean pass +clean-local: clean-qt-$(dir $(1)) +clean-qt-$(dir $(1)): $(LOCAL_BUILD_DIR)/$(dir $(1))Makefile + @$(QUIET_CMD_ECHO) " QT CLEAN $(dir $(1))" + $(Q)$(MAKE) SOURCES_DIR=$(SOURCES_DIR)/$(dir $(1)) \ + RELATIVE_DIR=$(RELATIVE_PREFIX)$(dir $(1)) \ + -C $(LOCAL_BUILD_DIR)/$(dir $(1)) clean + $(Q)rm $(LOCAL_BUILD_DIR)/$(dir $(1))Makefile +endef + +$(foreach pro,$(QT_PROJECTS), $(eval $(call qt_project_template,$(pro)))) +$(foreach pro,$(foreach dir,$(QT_SUBDIRS), $(wildcard $(dir)/*.pro)), $(eval $(call qt_project_template,$(pro)))) + +endif + #OMK:sources-list.omk@Makefile.rules.linux +# Rules that creates the list of files which are used during +# compilation. The list reflects conditional compilation depending on +# config.omk and other variables. + +SOURCES_LIST_FN=sources.txt +ifndef SOURCES_LIST +SOURCES_LIST_DIR:=$(RELATIVE_DIR) +SOURCES_LIST:=$(OUTPUT_DIR)/$(SOURCES_LIST_DIR)/$(SOURCES_LIST_FN) +SOURCES_LIST_D := $(LOCAL_BUILD_DIR)/$(SOURCES_LIST_FN).d +export SOURCES_LIST SOURCES_LIST_DIR SOURCES_LIST_D +endif + +ifneq ($(filter sources-list TAGS tags cscope,$(MAKECMDGOALS)),) +NEED_SOURCES_LIST=y +endif + +ifeq ($(NEED_SOURCES_LIST),y) # avoid execution of find command bellow if it is not useful +.PHONY: sources-list +sources-list: $(SOURCES_LIST) + +$(SOURCES_LIST): $(CONFIG_FILES) $(shell find -name $(MAKEFILE_OMK)) + @$(call mkdir_def,$(dir $(SOURCES_LIST_D))) + @echo -n "" > "$(SOURCES_LIST).tmp" + @echo -n "" > "$(SOURCES_LIST_D).tmp" + @$(MAKE) --no-print-directory sources-list-pass + @echo "# Automatically generated list of files in '$(RELATIVE_DIR)' that are used during OMK compilation" > "$(SOURCES_LIST).tmp2" + @cat "$(SOURCES_LIST).tmp"|sort|uniq >> "$(SOURCES_LIST).tmp2" + @rm "$(SOURCES_LIST).tmp" + @mv "$(SOURCES_LIST).tmp2" "$(SOURCES_LIST)" + @echo "$(SOURCES_LIST): \\" > "$(SOURCES_LIST_D).tmp2" + @cat "$(SOURCES_LIST_D).tmp"|grep -v "$(SOURCES_LIST_D).tmp"|sort|uniq|\ + $(SED4OMK) -e 's/$$/\\/' >> "$(SOURCES_LIST_D).tmp2" + @rm "$(SOURCES_LIST_D).tmp" + @mv "$(SOURCES_LIST_D).tmp2" "$(SOURCES_LIST_D)" +endif + +$(eval $(call omk_pass_template,sources-list-pass,$$(LOCAL_BUILD_DIR),,always)) + +sources-list-pass-local: + @$(foreach m,$(MAKEFILE_LIST),echo ' $(m)' >> "$(SOURCES_LIST_D).tmp";) + @$(foreach h,$(include_HEADERS) $(nobase_include_HEADERS) $(kernel_HEADERS),\ + echo "$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))$(h)" >> "$(SOURCES_LIST).tmp";) + @$(foreach ch,$(config_include_HEADERS), \ + echo "$(USER_INCLUDE_DIR:$(OUTPUT_DIR)/$(addsuffix /,$(SOURCES_LIST_DIR))%=%)/$(ch)" >> "$(SOURCES_LIST).tmp";) + @$(foreach h,$(renamed_include_HEADERS),echo '$(h)'|$(SED4OMK) -e 's|\(.*\)->.*|$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))\1|' >> "$(SOURCES_LIST).tmp";) + @$(foreach bin,$(lib_LIBRARIES) $(shared_LIBRARIES) $(bin_PROGRAMS) $(test_PROGRAMS) $(utils_PROGRAMS) \ + $(kernel_LIBRARIES) $(rtlinux_LIBRARIES) $(kernel_MODULES),\ + $(foreach src,$(filter-out %.o,$($(bin)_SOURCES)),echo "$(addsuffix /,$(RELATIVE_DIR:$(SOURCES_LIST_DIR)/%=%))$(src)" >> "$(SOURCES_LIST).tmp";)) + +############ TAGS ########### + +ifeq ($(MAKECMDGOALS),TAGS) +ETAGS=etags +TAGS_CMD = $(ETAGS) +TAGS: $(SOURCES_LIST) + @$(MAKE) --no-print-directory do-tags +endif +ifeq ($(MAKECMDGOALS),tags) +CTAGS=ctags -N +TAGS_CMD = $(CTAGS) +tags: $(SOURCES_LIST) + @$(MAKE) --no-print-directory do-tags +endif +export TAGS_CMD + +ifeq ($(MAKECMDGOALS),do-tags) +.PHONY: do-tags +do-tags: $(shell $(SED4OMK) -e '/^\#/d' $(SOURCES_LIST)) + @$(QUIET_CMD_ECHO) " TAGS $(SOURCES_LIST_FN)" + $(Q)$(TAGS_CMD) $^ +endif + +############ CSCOPE ########### + +cscope: $(SOURCES_LIST) + @$(QUIET_CMD_ECHO) " CSCOPE < $(SOURCES_LIST_FN)" + $(Q)$(SED4OMK) -e '/^#/d' $(SOURCES_LIST) > cscope.files + $(Q)cscope -b -icscope.files +#FIXME: see doc to -i in cscope(1) diff --git a/host/app/Makefile b/host/app/Makefile new file mode 100644 index 0000000..b22a357 --- /dev/null +++ b/host/app/Makefile @@ -0,0 +1,14 @@ +# Generic directory or leaf node makefile for OCERA make framework + +ifndef MAKERULES_DIR +MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) ) +endif + +ifeq ($(MAKERULES_DIR),) +all : default +.DEFAULT:: + @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n" +else +include $(MAKERULES_DIR)/Makefile.rules +endif + diff --git a/host/app/Makefile.omk b/host/app/Makefile.omk new file mode 100644 index 0000000..698ecc2 --- /dev/null +++ b/host/app/Makefile.omk @@ -0,0 +1 @@ +SUBDIRS = usb_sendhex diff --git a/host/app/usb_sendhex/Makefile b/host/app/usb_sendhex/Makefile new file mode 100644 index 0000000..b22a357 --- /dev/null +++ b/host/app/usb_sendhex/Makefile @@ -0,0 +1,14 @@ +# Generic directory or leaf node makefile for OCERA make framework + +ifndef MAKERULES_DIR +MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) ) +endif + +ifeq ($(MAKERULES_DIR),) +all : default +.DEFAULT:: + @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n" +else +include $(MAKERULES_DIR)/Makefile.rules +endif + diff --git a/host/app/usb_sendhex/Makefile.omk b/host/app/usb_sendhex/Makefile.omk new file mode 100644 index 0000000..7bd55e2 --- /dev/null +++ b/host/app/usb_sendhex/Makefile.omk @@ -0,0 +1,11 @@ +default_CONFIG = CONFIG_APP_USB_SENDHEX=x + +ifeq ($(CONFIG_APP_USB_SENDHEX),y) + +bin_PROGRAMS = usb_sendhex + +usb_sendhex_SOURCES = usb_sendhex.c + +lib_LOADLIBES = usb + +endif #CONFIG_APP_USB_SENDHEX diff --git a/host/app/usb_sendhex/usb_sendhex.c b/host/app/usb_sendhex/usb_sendhex.c new file mode 100644 index 0000000..257c3ed --- /dev/null +++ b/host/app/usb_sendhex/usb_sendhex.c @@ -0,0 +1,1178 @@ +/* usb_sendhex - program for manage device firmware by USB + * R.Bartosinski (C)2004 + * + * Based on 'ul_sendhex' + * + * Version 1.1 - 2013/08/26 + */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include + +#if !defined(_WIN32) && !defined(__DJGPP__) && !defined(HAS_GETDELIM) +#define HAS_GETDELIM +#endif +#define HAS_GETOPT_LONG + +#define USB_DEV_VID 0xDEAD +#define USB_DEV_PID 0x1000 + +#define USB_TIMEOUT 500 + +// USB vendor defines + +#define USB_VENDOR_GET_CAPABILITIES 0x00 // get capabilities +#define USB_VENDOR_RESET_DEVICE 0x08 +// #define USB_VENDOR_SET_BYTE 0x10 +// #define USB_VENDOR_SET_WORD 0x20 +#define USB_VENDOR_GET_SET_MEMORY 0x30 +#define USB_VENDOR_ERASE_MEMORY 0x40 // erase memory for 1 Byte +#define USB_VENDOR_ERASE_1KB_MEMORY 0x48 // erase memory for 1 KB +#define USB_VENDOR_MASS_ERASE 0x50 // erase all device memory +#define USB_VENDOR_GOTO 0x60 +#define USB_VENDOR_CALL 0x70 +#define USB_VENDOR_GET_STATUS 0xF0 +#define USB_VENDOR_MASK 0xF8 // mask for vendor commands + +#define USB_VENDOR_MEMORY_BY_BULK 0x80 + +int vid = USB_DEV_VID; +int pid = USB_DEV_PID; +int upload_flg = 0; +int mem_type = 2; +unsigned long mem_start = 0; +unsigned long mem_length = 0xff00; +unsigned long max_block = 1024; +unsigned long go_addr = 3; +int go_flg = 0; +int call = 0xFFFF; +int arg = 0; +int call_flg = 0; +int reset_flg = 0; +int prt_modules = 0; +int debugk = 0; +int debugk_flg = 0; +int verbose = 0; +int wait_for_device_flg = 0; +char *file_format = NULL; +int regerase_flg = 0; +int masserase_flg = 0; +unsigned long masserase_mode = 0; + +/*****************************************************************************/ + +typedef struct tform_file +{ + unsigned char *buf; + int buf_len; + int buf_addr; + int buf_bytes; + FILE *file; + unsigned char *line_buf; + int line_addr; + int ext_base_addr; + int line_bytes; + int line_offs; + int start_addr; + int (*read)(struct tform_file *tform); + int (*done)(struct tform_file *tform); +} tform_file; + +/*****************************************************************************/ + +#if __BYTE_ORDER == __BIG_ENDIAN +uint16_t usb_swab16(uint16_t x) +{ + return x << 8 | x >> 8; +} +#else +uint16_t usb_swab16(uint16_t x) +{ + return x; +} +#endif + +#ifndef HAS_GETDELIM +int getdelim(char **line, size_t *linelen, char delim, FILE *F) +{ + char c; + size_t l = 0; + + do + { + if (l + 1 >= *linelen) + { + *linelen = l + 20; + + if (!*line) + *line = (char *)malloc(*linelen); + else + *line = (char *)realloc(*line, *linelen); + } + + c = fgetc(F); + + if (feof(F)) + { + if (l) + break; + else + return -1; + } + + if (c != '\r') + (*line)[l++] = c; + } + while (c != delim); + + (*line)[l] = 0; + return l; +} +#endif + +int get_hex(char **p, unsigned *v, int chars) +{ + unsigned u = 0; + char c; + *v = 0; + + while (**p == ' ') + (*p)++; + + while (chars--) + { + u <<= 4; + c = **p; + + if ((c >= '0') && (c <= '9')) + u += c - '0'; + else if ((c >= 'A') && (c <= 'F')) + u += c - 'A' + 10; + else + return -1; + + (*p)++; + }; + + *v = u; + + return 0; +} + +int tform_init(tform_file *tform, int buf_len) +{ + if (!buf_len) buf_len = 1024; + + tform->file = NULL; + tform->buf_len = buf_len; + tform->buf = malloc(tform->buf_len); + tform->buf_addr = 0; + tform->line_buf = NULL; + tform->line_offs = 0; + tform->line_bytes = 0; + tform->start_addr = -1; + tform->buf_bytes = 0; + tform->read = NULL; + tform->done = NULL; + tform->ext_base_addr = 0; + return 0; +} + +int tform_done(tform_file *tform) +{ + if (tform->done) + return tform->done(tform); + + fclose(tform->file); + + if (tform->buf) + free(tform->buf); + + if (tform->line_buf) + free(tform->line_buf); + + return 0; +}; + +int tform_read(tform_file *tform) +{ + return tform->read(tform); +} + +int tform_read_ihex(tform_file *tform) +{ + int cn, len = 0; + int addr = 0; + unsigned u, v; + char *p; + unsigned char *r; + char *line = NULL; + size_t line_len = 0; + + while (len < tform->buf_len) + { + if (!tform->line_bytes) + { + int checksum = 0; + int ihex_type = 0; + + tform->line_offs = 0; + + if (getdelim(&line, &line_len, '\n', tform->file) == -1) + break; + + p = line; + + if (*p++ != ':') + printf("tform_read : strange line %s\n", line); + else + { + if (get_hex(&p, &u, 2) < 0) + { + printf("tform_read_ihex : bad ihex cnt\n"); + return -1; + } + + checksum += cn = tform->line_bytes = u; + + if (!tform->line_buf) + tform->line_buf = malloc(cn); + else + tform->line_buf = realloc(tform->line_buf, cn); + + if (get_hex(&p, &u, 2) < 0) + { + printf("tform_read_ihex : bad ihex addr\n"); + return -1; + } + + if (get_hex(&p, &v, 2) < 0) + { + printf("tform_read_ihex : bad ihex addr\n"); + return -1; + } + + checksum += u + v; + tform->line_addr = (u << 8) + v; + + if (get_hex(&p, &u, 2) < 0) + { + printf("tform_read_ihex : bad ihex type\n"); + return -1; + } + + checksum += ihex_type = u; + + if ((ihex_type >= 0) && (ihex_type <= 5)) + { + r = tform->line_buf; + + while (cn--) + { + if (get_hex(&p, &u, 2) < 0) + { + printf("tform_read_ihex : bad ihex data\n"); + return -1; + } + + checksum += *r++ = u; + } + + if (get_hex(&p, &u, 2) < 0) + { + printf("tform_read_ihex : bad ihex csum\n"); + return -1; + } + + checksum += u; + + if (checksum & 0xff) + { + printf("tform_read_ihex : error ihex csum %d\n", + checksum); + return -1; + } + + while ((u = *p++)) if (u != ' ' && u != '\n' && u != '\r') + { + printf("tform_read_ihex : residual chars on line\n"); + return -1; + } + } + + if (ihex_type == 1) + { + tform->line_bytes = 0; + if(tform->start_addr == -1) + tform->start_addr = tform->line_addr; + } + + tform->line_addr+=tform->ext_base_addr; + + if((ihex_type >= 2) && (ihex_type <= 5)) + { + cn = tform->line_bytes; + r = tform->line_buf; + addr = 0; + + while(cn-- > 0) + { + addr <<= 8; + addr += *(r++); + } + if(ihex_type == 2) + tform->ext_base_addr=addr << 4; + else if(ihex_type == 4) + tform->ext_base_addr = addr << 16; + else if(ihex_type == 5) + tform->start_addr = addr; + + tform->line_bytes = 0; + } + } + } + + if (tform->line_bytes) + { + if (!len) + addr = tform->buf_addr = tform->line_addr + tform->line_offs; + else if (addr != tform->line_addr + tform->line_offs) + break; + + cn = tform->line_bytes - tform->line_offs; + + if (cn + len > tform->buf_len) cn = tform->buf_len - len; + + memcpy(tform->buf + len, tform->line_buf + tform->line_offs, cn); + len += cn; + addr += cn; + tform->line_offs += cn; + + if (tform->line_bytes == tform->line_offs) + tform->line_bytes = 0; + } + } + + tform->buf_bytes = len; + + return len; +} + +int tform_read_binary(tform_file *tform) +{ + int len = 0; + + tform->buf_addr += tform->buf_bytes; + len = fread(tform->buf, 1, tform->buf_len, tform->file); + + if (len < 0) + { + perror("tform_read_binary : read error"); + return -1; + } + + tform->buf_bytes = len; + return len; +} + +int tform_open(tform_file *tform, char *file_name, + char *format, int buf_len, int wr_fl) +{ + FILE *file; + + if (!format || !strcmp("ihex", format)) + { + if ((file = fopen(file_name, "r")) == NULL) + { + perror("download_file : hex file open"); + return -1; + } + + tform_init(tform, buf_len); + tform->file = file; + tform->read = tform_read_ihex; + } + else if (!strcmp("binary", format)) + { + if ((file = fopen(file_name, "rb")) == NULL) + { + perror("download_file : binary file open"); + return -1; + }; + + tform_init(tform, buf_len); + tform->file = file; + tform->read = tform_read_binary; + } + else + { + fprintf(stderr, "requested unknown format %s\n", format); + return -1; + } + + return 1; +} + + +/*****************************************************************************/ +/*****************************************************************************/ +/*****************************************************************************/ +/* USB functions */ +void print_devices(void) +{ + struct usb_bus *bus; + struct usb_device *dev; + int i = 0; + + usb_init(); // NO for more devices + usb_find_busses(); + usb_find_devices(); + + printf("All connected usb devices\n"); + printf(" bus/device idVendor/idProduct\n"); + + for (bus = usb_busses; bus; bus = bus->next) + { + for (dev = bus->devices; dev; dev = dev->next) + { + i++; + printf(" %s/%s 0x%04X/0x%04X\n", bus->dirname, dev->filename, dev->descriptor.idVendor, dev->descriptor.idProduct); + } + } + + if (!i) + printf(" -- no device.\n"); +} + +struct usb_device *find_usb_device(int vendor, int product) +{ + struct usb_bus *bus; + struct usb_device *dev; + + for (bus = usb_busses; bus; bus = bus->next) + { + for (dev = bus->devices; dev; dev = dev->next) + { + if ((dev->descriptor.idVendor == vendor) && + (dev->descriptor.idProduct) == product) + return dev; + } + } + + return NULL; +} + +usb_dev_handle *usb_open_device(int uvid, int upid) +{ + struct usb_device *dev; + usb_dev_handle *hdev; + + usb_init(); // NO for more devices + usb_find_busses(); + usb_find_devices(); + + dev = find_usb_device(uvid, upid); + + if (!dev) + { + if (verbose) + printf("!!! Cannot find device 0x%04X:0x%04X\n", uvid, upid); + + return NULL; + } + + if ((hdev = usb_open(dev)) == NULL) + { + if (verbose) + printf("!!! USB device wasn't opened !!!\n"); + + return NULL; + } + + usb_claim_interface(hdev, 0); + + if (verbose) + printf(" USB Device 0x%04X:0x%04X '%s' is open.\n", uvid, upid, dev->filename); + + return hdev; +} + +int usb_close_device(usb_dev_handle *hdev) +{ + int bRes = 1; + usb_release_interface(hdev, 0); + bRes = usb_close(hdev); + + if (bRes && verbose) + printf("!!! USB Device wasn't closed !!!\n"); + + return bRes; +} + +/*****************************************************************************/ +/*****************************************************************************/ +/*****************************************************************************/ + +int download_file(char *file_name, char *format) +{ + usb_dev_handle *hdev; + tform_file tform; + int len; + int i; + int ret = 0; +// int stamp; + unsigned long shift_addr = 0; + + struct timeval time1, time2; + struct timezone tz; + + shift_addr = mem_start; + + hdev = usb_open_device(vid, pid); + + if (!hdev) + { + perror("download_file : usb device open failed"); + return -1; + } + + /* ul_drv_debflg(ul_fd,0x11); */ /* 0x9 0x11 */ + + if (tform_open(&tform, file_name, format, max_block, 0) < 0) + { + usb_close_device(hdev); + return -1; + } + + gettimeofday(&time1, &tz); + + do + { + len = tform_read(&tform); + + if (!len) break; + + if (len < 0) + { + perror("download_file : ihex"); + ret = -1; + break; + } + + printf("addr %4lX len %4X\n", tform.buf_addr + shift_addr, len); + + /* send data */ + + // if((stamp=ul_new_memrq_write(ul_fd,module,mem_type, + // tform.buf_addr+shift_addr,len,tform.buf))<0) + // { printf("download_file : send message error\n"); + // ret=-1; break; + // }; + i = 3; + + do + { + ret = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, USB_VENDOR_GET_SET_MEMORY | mem_type /*USB_VENDOR_TARGET_XDATA*/, + (unsigned long)(tform.buf_addr + shift_addr) & 0xffff, (((unsigned long)(tform.buf_addr + shift_addr)) >> 16) & 0xffff, (void *)tform.buf, len, 150 + len); //USB_TIMEOUT); +// ret = usb_bulk_write( hdev, USB_ENDPOINT_IN | 0x02, buf, len, 1000); + + if (verbose && ret < 0) printf("Mem read error %d - again\n", ret); + + i--; + } + while (ret < 0 && i); + + } + while (1); + + gettimeofday(&time2, &tz); + + if (verbose) + { + long dus = (time2.tv_sec * 1000000 + time2.tv_usec) - (time1.tv_sec * 1000000 + time1.tv_usec); + printf("Upload time %lu.%lu s\n", dus / 1000000, dus % 1000000); + } + + + if (tform.start_addr != -1) + printf("Found start address %4X\n", tform.start_addr); + + tform_done(&tform); + usb_close_device(hdev); + return ret; +} + +int send_cmd_call(int cmd, int val) +{ + int ret, resp_val; + usb_dev_handle *hdev; + char resp[10]; + + hdev = usb_open_device(vid, pid); + + if (!hdev) + { + perror("send_cmd_call : USB open failed"); + return -1; + } + + ret = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_VENDOR_CALL, val & 0xffff, cmd & 0xffff, resp, sizeof(resp), USB_TIMEOUT); + resp_val = usb_swab16(*((uint16_t *)(resp))); + + if (ret < 0) + printf("Call %4X (%4X) ERROR %d: %s\n", (uint16_t)(cmd & 0xffff), (uint16_t)(val & 0xffff), ret, usb_strerror()); + else + printf("Call %4X (%4X): %4X\n", (uint16_t)(cmd & 0xffff), (uint16_t)(val & 0xffff), resp_val); + + usb_close_device(hdev); + return ret; +} + +int send_cmd_go(unsigned long addr) +{ + int ret; + usb_dev_handle *hdev; + + hdev = usb_open_device(vid, pid); + + if (!hdev) + { + perror("send_cmd_go : USB open failed"); + return -1; + } + + ret = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_VENDOR_GOTO, addr & 0xffff, (addr >> 16) & 0xffff, NULL, 0, USB_TIMEOUT); + + if (ret < 0) printf("Goto to %4lX ERROR %d: %s\n", addr, ret, usb_strerror()); + else printf("Goto to %4lX OK\n", addr); + + usb_close_device(hdev); + return ret; +} + +int send_cmd_reset() +{ + int ret; + usb_dev_handle *hdev; + + hdev = usb_open_device(vid, pid); + + if (!hdev) + { + perror("send_cmd_reset : USB open failed"); + return -1; + } + + ret = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_VENDOR_RESET_DEVICE, 0, 0, NULL, 0, USB_TIMEOUT); + + if (ret < 0) + printf("Reset device ERROR %d: %s\n", ret, usb_strerror()); + else + printf("Reset OK\n"); + + usb_close_device(hdev); + return ret; +}; + + +int send_cmd_regerase(unsigned long addr, unsigned long len) +{ + int ret; + usb_dev_handle *hdev; + + hdev = usb_open_device(vid, pid); + + if (!hdev) + { + perror("send_cmd_regerase : USB open failed"); + return -1; + } + + if (addr + len < 0x10000) + { + ret = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_VENDOR_ERASE_MEMORY, addr, len, NULL, 0, USB_TIMEOUT * 5); + } + else + { + len += addr & 0x3ff; + ret = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_VENDOR_ERASE_1KB_MEMORY, addr >> 10, len >> 10, NULL, 0, USB_TIMEOUT * 10); + } + + if (ret < 0) + printf("Region Erase from %4lX ERROR %d: %s\n", addr, ret, usb_strerror()); + else + printf("Region Erase from %4lX OK\n", addr); + + usb_close_device(hdev); + return ret; +}; + + + +int send_cmd_masserase(unsigned long mode) +{ + int ret; + usb_dev_handle *hdev; + + hdev = usb_open_device(vid, pid); + + if (!hdev) + { + perror("send_cmd_masserase : USB open failed"); + return -1; + }; + + ret = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_VENDOR_MASS_ERASE, mode & 0xffff, (mode >> 16) & 0xffff, NULL, 0, USB_TIMEOUT * 20); + + if (ret < 0) + printf("Mass Erase to %4lX ERROR %d: %s\n", mode, ret, usb_strerror()); + else printf("Mass Erase to %4lX OK\n", mode); + + usb_close_device(hdev); + return ret; +}; + + +int upload_file(char *file_name, char *format) +{ + usb_dev_handle *hdev; + FILE *file; + int ret = 0; + unsigned char buf[0x400]; + unsigned char *p; + char *mode = "w"; + enum {fmt_ihex, fmt_binary, fmt_dump} fmt; + int i, l, csum; + unsigned long mem_adr = mem_start; + unsigned long mem_len = mem_length; + unsigned long len; + unsigned long ext_addr = 0; /* for Intel HEX format */ + + struct timeval time1, time2; + struct timezone tz; + + if (max_block > 0x400) + max_block = 0x400; + + if (!format || !strcmp("ihex", format)) + fmt = fmt_ihex; + else if (!strcmp("binary", format)) + { + mode = "wb"; + fmt = fmt_binary; + } + else if (!strcmp("dump", format)) + fmt = fmt_dump; + else + { + fprintf(stderr, "requested unknown format %s\n", format); + return -1; + } + + if (!strcmp(file_name, "-")) + file_name = NULL; + + hdev = usb_open_device(vid, pid); + + if (!hdev) + { + perror("upload_file : open failed"); + return -1; + }; + + /* ul_drv_debflg(ul_fd,0x11); */ /* 0x9 0x11 */ + + if (file_name) + { + if ((file = fopen(file_name, mode)) == NULL) + { + perror("upload_file : file open"); + usb_close_device(hdev); + return -1; + } + } + else file = stdout; + + gettimeofday(&time1, &tz); + +// ret = usb_control_msg( hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_VENDOR_MEMORY_BY_BULK | USB_VENDOR_TARGET_XDATA, +// mem_adr & 0xffff, mem_len, NULL, 0, 2000); //USB_TIMEOUT); + + if (ret < 0) + { + printf("ERR ctrl msg\n"); + mem_len = 0; + } + + while (mem_len) + { + len = mem_len < max_block ? mem_len : max_block; + /* read data */ + i = 3; + + do + { + ret = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_VENDOR_GET_SET_MEMORY | mem_type /*USB_VENDOR_TARGET_XDATA*/, + mem_adr & 0xffff, (mem_adr >> 16) & 0xffff, (void *)buf, len, 100 + len); //USB_TIMEOUT); +// ret = usb_bulk_read( hdev, USB_ENDPOINT_IN | 0x02, buf, len, 1000); + + if (verbose && ret < 0) + printf("Mem read error - again\n"); + + i--; + } + while (ret < 0 && i); + + if (ret < 0) + { + printf("Mem read returns %d: %s\n", ret, usb_strerror()); + break; + } + + if (file_name) printf("%04lX\n", mem_adr); + + switch (fmt) + { + case fmt_ihex: + p = buf; + i = 0; + + while (i < len) + { + unsigned long a = mem_adr + i; + + if ((a & ~0xFFFF) != ext_addr) + { + unsigned long val; + unsigned char b; + + ext_addr = a & ~0xFFFF; + val = ext_addr >> 16; + l = 2; + val >>= 16; + while (val != 0) + { + val >>= 8; + l++; + } + fprintf(file, ":%02X000004", l); + csum = l + 4; + val = ext_addr >> 16; + while (l--) + { + b = (val >> (8 * l)) & 0xff; + fprintf(file, "%02X", b); + csum += b; + } + fprintf(file, "%02X\n", (-csum) & 0xFF); + } + + l = len - i; + + if (l > 16) l = 16; + + csum = l + a + (a >> 8); + fprintf(file, ":%02X%04lX00", l, a & 0xffff); + + while (l--) + { + fprintf(file, "%02X", buf[i]); + csum += buf[i++]; + }; + + fprintf(file, "%02X\n", (-csum) & 0xFF); + } + + break; + + case fmt_binary: + if (fwrite(buf, len, 1, file) != 1) + { + perror("upload_file : file write"); + return -1; + } + + break; + + case fmt_dump: + i = 0; + p = buf; + + while (i < len) + { + if (i & 0xf) printf(" %02X", *(p++)); + else printf(i ? "\n%04lX:%02X" : "%04lX:%02X", mem_adr + i, *(p++)); + + i++; + } + + printf("\n"); + break; + } + + mem_adr += len; + mem_len -= len; + } + + gettimeofday(&time2, &tz); + + if (verbose) + { + long dus = (time2.tv_sec * 1000000 + time2.tv_usec) - (time1.tv_sec * 1000000 + time1.tv_usec); + printf("Upload time %lu.%lu s\n", dus / 1000000, dus % 1000000); + } + + if (fmt == fmt_ihex) + fprintf(file, ":00000001FF\n"); + + if (file_name) + fclose(file); + + usb_close_device(hdev); + + return 0; +}; + +static int wait_for_device(int timeout) +{ + usb_dev_handle *hdev; + + while (timeout) + { + hdev = usb_open_device(vid, pid); + + if (hdev) + break; + +#ifdef _WIN32 + Sleep(1000); +#else + sleep(1); +#endif + timeout--; + } + + return timeout ? 0 : -1; +} + +/*****************************************************************************/ +/*****************************************************************************/ +/*****************************************************************************/ + +static void +usage(void) +{ + printf("Usage: usb_sendhex \n"); + printf(" -d, --vid device vendor id (VID) [0x%04X]\n", USB_DEV_VID); + printf(" -i, --pid product id (PID) [0x%04X]\n", USB_DEV_PID); + printf(" -t, --type target module memory space\n"); + printf(" -s, --start start address of transfer\n"); + printf(" -l, --length length of upload block\n"); + printf(" -b, --block maximal block length\n"); + printf(" -c, --call vendor custom call\n"); + printf(" -a, --argument argument for vendor custom call\n"); + printf(" -g, --go start program from address\n"); + printf(" -r, --reset reset before download\n"); + printf(" -E, --mass-erase full device erase\n"); + printf(" -e, --erase erase region defined by -s -l\n"); + printf(" -u, --upload upload memory block [download]\n"); + printf(" -w, --wait wait for device to be on\n"); + printf(" -f, --format format of data file [ihex]\n"); + printf(" -p, --print print devices\n"); + printf(" --debug-kernel flags to debug kernel\n"); + printf(" -v, --verbose verbose program\n"); + printf(" -V, --version show version\n"); + printf(" -h, --help this usage screen\n"); +} + +int main(int argc, char *argv[]) +{ + static struct option long_opts[] = + { + { "vid", 1, 0, 'd' }, + { "pid", 1, 0, 'i' }, + { "type", 1, 0, 't' }, + { "start", 1, 0, 's' }, + { "length", 1, 0, 'l' }, + { "block", 1, 0, 'b' }, + { "call", 1, 0, 'c' }, + { "argument", 1, 0, 'a' }, + { "go", 1, 0, 'g' }, + { "reset", 0, 0, 'r' }, + { "mass-erase", 1, 0, 'E' }, + { "erase", 0, 0, 'e' }, + { "upload", 0, 0, 'u' }, + { "wait", 0, 0, 'w' }, + { "format", 1, 0, 'f' }, + { "print", 0, 0, 'p' }, + { "verbose", 0, 0, 'v' }, + { "version", 0, 0, 'V' }, + { "help", 0, 0, 'h' }, + { "debug-kernel", 1, 0, 'D' }, + { 0, 0, 0, 0} + }; + int opt; + +#ifndef HAS_GETOPT_LONG + while ((opt = getopt(argc, argv, "d:i:t:s:l:b:c:a::g:rE:euwf:pvVhD:")) != EOF) +#else + while ((opt = getopt_long(argc, argv, "d:i:t:s:l:b:c:a:g:rE:euwf:pvVh", &long_opts[0], NULL)) != EOF) +#endif + + switch (opt) + { + char *p; + case 'd': + vid = strtol(optarg, &p, 16); + + if (!p || (p == optarg)) + { + printf("usb_sendhex : vendor ID is not hexadecimal number\n"); + exit(1); + } + + if (*p == ':') + { + char *r = p + 1; + pid = strtol(r, &p, 16); + + if (!p || (p == r)) + { + printf("usb_sendhex : product ID is not hexadecimal number\n"); + exit(1); + } + } + + break; + case 'i': + pid = strtol(optarg, &p, 16); + break; + case 't': + mem_type = strtol(optarg, NULL, 0); + break; + case 's': + mem_start = strtoul(optarg, NULL, 0); + break; + case 'l': + mem_length = strtoul(optarg, NULL, 0); + break; + case 'b': + max_block = strtoul(optarg, NULL, 0); + break; + case 'c': + call = strtoul(optarg, NULL, 0); + call_flg = 1; + break; + case 'a': + arg = strtoul(optarg, NULL, 0); + break; + case 'g': + go_addr = strtoul(optarg, NULL, 0); + go_flg = 1; + break; + case 'r': + reset_flg = 1; + break; + case 'E': + masserase_mode = strtoul(optarg, NULL, 0); + masserase_flg = 1; + break; + case 'e': + regerase_flg = 1; + break; + case 'u': + upload_flg = 1; + break; + case 'w': + wait_for_device_flg = 1; + break; + case 'f': + file_format = optarg; + break; + case 'p': + prt_modules = 1; + break; + case 'D': + debugk = strtol(optarg, NULL, 0); + debugk_flg = 1; + break; + case 'v': + verbose = 1; + break; + case 'V': + fputs("USB sendhex v.1.1\n", stdout); + exit(0); + case 'h': + default: + usage(); + exit(opt == 'h' ? 0 : 1); + } + + if ((optind >= argc) && !go_flg && !call_flg && !prt_modules && !debugk_flg + && !masserase_flg && !regerase_flg && !reset_flg) + { + usage(); + exit(1); + } + + if (prt_modules) + print_devices(); + + if (wait_for_device_flg) + if (wait_for_device(100) < 0) + exit(2); + + if (reset_flg) + send_cmd_reset(); + + if (regerase_flg) + send_cmd_regerase(mem_start, mem_length); + + if (masserase_flg) + send_cmd_masserase(masserase_mode); + + if (!upload_flg) + { + while (optind < argc) + if (download_file(argv[optind++], file_format) < 0) + exit(2); + } + else + { + if (optind + 1 != argc) + { + printf("upload_file : needs exactly one filename\n"); + exit(1); + } + + if (upload_file(argv[optind], file_format) < 0) + exit(2); + } + + if (call_flg) + { + if (send_cmd_call(call, arg) < 0) + exit(2); + } + + if (go_flg) + { + if (send_cmd_go(go_addr) < 0) + exit(2); + } + + return 0; +} diff --git a/host/config.omk b/host/config.omk new file mode 100644 index 0000000..1f47def --- /dev/null +++ b/host/config.omk @@ -0,0 +1,12 @@ +LN_HEADERS=y + +CONFIG_APP_ROCON_CMD=y +CONFIG_APP_USB_SENDHEX=y +CONFIG_APP_LPC21LISP=y + +# Fails +CONFIG_OC_ULUT=n +CONFIG_OC_ULUTKERN=n +CONFIG_OC_I2C_CHIP_C552=n +CONFIG_OC_CMDPROC=n +CONFIG_MISC_LIBRARY=n -- 2.39.2