2012-02-22 7 views
1

Javaネイティブインターフェイスを使用するOSGIバンドルに取り組んでいます。私は.soのモジュールを構築するために、このメイクファイルを使用します。OSGIバンドルにCコードを含めるには

#!/usr/bin/make -f 
# 
# Makefile for native stuff 
# 

# c files to compile 
C_SOURCES := sqrt.c 

# the name of the library to build 
LIBNAME  := sqrt 

C_SOURCE_DIR := src 
C_GENSOURCE_DIR := src 

TARGET_DIR := ../../../target 
C_BUILD_DIR = $(TARGET_DIR)/native 
JAVA_BUILD_DIR = $(TARGET_DIR)/classes 

# the name of the file we build 
TARGET  = $(JAVA_BUILD_DIR)/META-INF/lib/$(LIB_PREFIX)$(LIBNAME)$(LIB_EXTN) 

# find the jdk. if this doesn't work for you, define JAVA_HOME in your 
# environment or on the make command line 
JAVA_HOME ?= /opt/jdk1.7.0_02 

# classpath for javah 
JAVAH_CLASSPATH = `cat $(TARGET_DIR)/compile-classpath` 

# tools and options 
CFLAGS = -Wall -fpic 
CPPFLAGS = -I$(C_SOURCE_DIR) -I$(C_GENSOURCE_DIR) -Iinclude \ 
    -I$(JAVA_HOME)/include 
JAVAH = /opt/jdk1.7.0_02/bin/javah 
JAVAH_FLAGS += -classpath $(JAVAH_CLASSPATH) 
JAVAH_CMD = $(JAVAH) $(JAVAH_FLAGS) $(OUTPUT_OPTION) 
LDFLAGS = -shared 
LINK.so = $(CC) $(LDFLAGS) $(LD_LIBS) 

ifdef DEBUG 
CFLAGS += -g 
LDFLAGS += -g 
endif 

# os-dependent bits 
UNAME := $(shell uname) 

ifeq ($(UNAME),Linux) 
LIB_PREFIX = lib 
LIB_EXTN = .so 
CPPFLAGS += -I$(JAVA_HOME)/include/linux 
else 
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN) 
LIB_PREFIX = 
LIB_EXTN = .dll 
CPPFLAGS += -I$(JAVA_HOME)/include/win32 
else 
f := $(error Platform $(UNAME) not supported) 
endif 
endif 

# we look in $(C_SOURCE_DIR) for c files... 
vpath %.c $(C_SOURCE_DIR) 

# convenience variables 
C_OBJFILES = $(addprefix $(C_BUILD_DIR)/,$(subst .c,.o,$(C_SOURCES))) 

# default target 
all: $(TARGET) 

# rule to compile the .c files 
$(C_BUILD_DIR)/%.o: %.c 
    @mkdir -p `dirname [email protected]` 
    $(COMPILE.c) $(OUTPUT_OPTION) $< 

# link the C objects into a shared library 
$(TARGET): $(C_OBJFILES) $(LDLIBS) 
    @mkdir -p `dirname [email protected]` 
    $(LINK.so) $(OUTPUT_OPTION) $^ 

# a rule to build the .h file with javah      
$(C_GENSOURCE_DIR)/org_DX_57_osgi_NB_27_impl_Sqrt.h: $(JAVA_BUILD_DIR)/org/DX_57/osgi/NB_27/impl/Sqrt.class 
    rm -f [email protected]     
    $(JAVAH) $(JAVAH_FLAGS) $(OUTPUT_OPTION) org.DX_57.osgi.NB_27.impl.Sqrt 

# the .o file depends on the .h file 
$(C_BUILD_DIR)/sqrt.o: $(C_GENSOURCE_DIR)/org_DX_57_osgi_NB_27_impl_Sqrt.h 

clean:: 
    rm -f $(C_OBJFILES) 
    rm -f $(TARGET) 
    rm -f $(C_BUILD_DIR)/jnirules.mak 

このMakefileは成功したヘッダファイルを生成し、.oのファイルをコンパイルします。問題は、バンドルがコンパイルされるときに.soファイルがパッケージ化されていないということです。 これはPOMファイルの内容です:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
     <groupId>org.DX_57.osgi.NB_27</groupId> 
     <artifactId>NB_27</artifactId> 
     <version>1.0-SNAPSHOT</version> 
     <relativePath>../pom.xml</relativePath> 
    </parent> 
    <artifactId>NB_27-impl</artifactId> 
    <packaging>bundle</packaging> 
    <name>NB_27-impl</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.felix</groupId> 
      <artifactId>org.osgi.core</artifactId> 
      <version>1.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>${pom.groupId}</groupId> 
      <artifactId>NB_27-api</artifactId> 
      <version>1.0-SNAPSHOT</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
     <plugin> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <executions> 
      <execution> 
      <id>build-native</id> 
      <phase>process-classes</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
      <configuration> 
       <tasks> 
       <property name="native.classpath" refid="maven.compile.classpath" /> 
       <echo file="${project.build.directory}/compile-classpath" message="${native.classpath}" /> 
       <exec dir="src/main/native" executable="make" failonerror="true" /> 
       </tasks> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 

      <plugin> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-bundle-plugin</artifactId> 
       <extensions>true</extensions> 
       <configuration> 
        <instructions> 
         <Import-Package>${pom.groupId}.api, org.osgi.framework, org.apache.commons.collections, org.apache.commons.collections.buffer</Import-Package> 
         <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName> 
         <Bundle-Activator>${pom.groupId}.impl.NetworkBridgeApp</Bundle-Activator> 
        </instructions> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <groupId>org.DX_57.osgi.NB_27</groupId> 
</project> 

私はMavenのバンドル・プラグインとのmaven-組立・プラグインネイティブライブラリとのmaven-コンパイラプラグインがバンドルに含まれて交換してください

。どのようにOSGIバンドルを作成し、ビルド時にネイティブライブラリを含めることができますか?

答えて

2

Maven Bundle Pluginにネイティブコードの特定の規定があるとは思えません。あなたが必要と思われるものはありません。あなたが必要とするすべてのサポートはすでにあります。あなたは<Include-Resource>命令を使用してネイティブライブラリが含まれるように

  • を必要とし、
  • ますあなたが手でマニフェストを構築するだろうというときと同じように、説明書にBundle-NativeCodeヘッダーが含まれます。
関連する問題