nvidia-driver.eclass: Use next gen version of readme.gentoo eclass
[gentoo.git] / eclass / java-osgi.eclass
1 # Copyright 2007-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: java-osgi.eclass
6 # @MAINTAINER:
7 # java@gentoo.org
8 # @AUTHOR:
9 # Java maintainers (java@gentoo.org)
10 # @BLURB: Java OSGi eclass
11 # @DESCRIPTION:
12 # This eclass provides functionality which is used by packages that need to be
13 # OSGi compliant. This means that the generated jars will have special headers
14 # in their manifests. Currently this is used only by Eclipse-3.3 - later we
15 # could extend this so that Gentoo Java system would be fully OSGi compliant.
16
17 inherit java-utils-2
18
19 # @ECLASS-VARIABLE: _OSGI_T
20 # @INTERNAL
21 # @DESCRIPTION:
22 # We define _OSGI_T so that it does not contain a slash at the end.
23 # According to Paludis guys, there is currently a proposal for EAPIs that
24 # would require all variables to end with a slash.
25 _OSGI_T="${T/%\//}"
26
27 # must get Diego to commit something like this to portability.eclass
28 _canonicalise() {
29         if type -p realpath > /dev/null; then
30                 realpath "${@}"
31         elif type -p readlink > /dev/null; then
32                 readlink -f "${@}"
33         else
34                 # can't die, subshell
35                 eerror "No readlink nor realpath found, cannot canonicalise"
36         fi
37 }
38
39 # @FUNCTION: _java-osgi_plugin
40 # @USAGE: <plugin name>
41 # @INTERNAL
42 # @DESCRIPTION:
43 # This is an internal function, not to be called directly.
44 #
45 # @CODE
46 #       _java-osgi_plugin "JSch"
47 # @CODE
48 #
49 # @param $1 - bundle name
50 _java-osgi_plugin() {
51         # We hardcode Gentoo as the vendor name
52
53         cat > "${_OSGI_T}/tmp_jar/plugin.properties" <<-EOF
54         bundleName="${1}"
55         vendorName="Gentoo"
56         EOF
57 }
58
59 # @FUNCTION: _java-osgi_makejar
60 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
61 # @INTERNAL
62 # @DESCRIPTION:
63 # This is an internal function, not to be called directly.
64 #
65 # @CODE
66 #       _java-osgi_makejar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
67 # @CODE
68 #
69 # @param $1 - name of jar to repackage with OSGi
70 # @param $2 - bundle symbolic name
71 # @param $3 - bundle name
72 # @param $4 - export-package header
73 _java-osgi_makejar() {
74         debug-print-function ${FUNCNAME} "$@"
75
76         (( ${#} < 4 )) && die "Four arguments are needed for _java-osgi_makejar()"
77
78         local absoluteJarPath="$(_canonicalise ${1})"
79         local jarName="$(basename ${1})"
80
81         mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
82         [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
83
84         cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
85                  || die "Unable to uncompress correctly the original jar"
86
87         cat > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" <<-EOF
88         Manifest-Version: 1.0
89         Bundle-ManifestVersion: 2
90         Bundle-Name: %bundleName
91         Bundle-Vendor: %vendorName
92         Bundle-Localization: plugin
93         Bundle-SymbolicName: ${2}
94         Bundle-Version: ${PV}
95         Export-Package: ${4}
96         EOF
97
98         _java-osgi_plugin "${3}"
99
100         jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
101                 -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
102         rm -rf "${_OSGI_T}/tmp_jar"
103 }
104
105 # @FUNCTION: @java-osgi_dojar
106 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
107 # @DESCRIPTION:
108 # Rewrites a jar, and produce an OSGi compliant jar from arguments given on the command line.
109 # The arguments given correspond to the minimal set of headers
110 # that must be present on a Manifest file of an OSGi package.
111 # If you need more headers, you should use the *-fromfile functions below,
112 # that create the Manifest from a file.
113 # It will call java-pkg_dojar at the end.
114 #
115 # @CODE
116 #       java-osgi_dojar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
117 # @CODE
118 #
119 # @param $1 - name of jar to repackage with OSGi
120 # @param $2 - bundle symbolic name
121 # @param $3 - bundle name
122 # @param $4 - export-package-header
123 java-osgi_dojar() {
124         debug-print-function ${FUNCNAME} "$@"
125         local jarName="$(basename ${1})"
126         _java-osgi_makejar "$@"
127         java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"
128 }
129
130 # @FUNCTION: java-osgi_newjar
131 # @USAGE: <jar name> <symbolic name> <bundle name> <header name>
132 # @DESCRIPTION:
133 # Rewrites a jar, and produce an OSGi compliant jar.
134 # The arguments given correspond to the minimal set of headers
135 # that must be present on a Manifest file of an OSGi package.
136 # If you need more headers, you should use the *-fromfile functions below,
137 # that create the Manifest from a file.
138 # It will call java-pkg_newjar at the end.
139 #
140 # @CODE
141 #       java-osgi_newjar "dist/${PN}.jar" "com.jcraft.jsch" "JSch" "com.jcraft.jsch, com.jcraft.jsch.jce;x-internal:=true"
142 # @CODE
143 #
144 # @param $1 - name of jar to repackage with OSGi
145 # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
146 # @param $3 - bundle symbolic name
147 # @param $4 - bundle name
148 # @param $5 - export-package header
149 java-osgi_newjar() {
150         debug-print-function ${FUNCNAME} "$@"
151         local jarName="$(basename $1)"
152
153         if (( ${#} > 4 )); then
154                 _java-osgi_makejar "${1}" "${3}" "${4}" "${5}"
155                 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
156         else
157                 _java-osgi_makejar "$@"
158                 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
159         fi
160 }
161
162 # @FUNCTION:_java-osgi_makejar-fromfile
163 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
164 # @INTERNAL
165 # @DESCRIPTION:
166 # This is an internal function, not to be called directly.
167 #
168 # @CODE
169 #       _java-osgi_makejar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "JSch" 1
170 # @CODE
171 #
172 # @param $1 - name of jar to repackage with OSGi
173 # @param $2 - path to the Manifest file
174 # @param $3 - bundle name
175 # @param $4 - automatic version rewriting (0 or 1)
176 _java-osgi_makejar-fromfile() {
177         debug-print-function ${FUNCNAME} "$@"
178
179         ((${#} < 4)) && die "Four arguments are needed for _java-osgi_makejar-fromfile()"
180
181         local absoluteJarPath="$(_canonicalise ${1})"
182         local jarName="$(basename ${1})"
183
184         mkdir "${_OSGI_T}/tmp_jar" || die "Unable to create directory ${_OSGI_T}/tmp_jar"
185         [[ -d "${_OSGI_T}/osgi" ]] || mkdir "${_OSGI_T}/osgi" || die "Unable to create directory ${_OSGI_T}/osgi"
186
187         cd "${_OSGI_T}/tmp_jar" && jar xf "${absoluteJarPath}" && cd - > /dev/null \
188                 || die "Unable to uncompress correctly the original jar"
189
190         [[ -e "${2}" ]] || die "Manifest file ${2} not found"
191
192         # We automatically change the version if automatic version rewriting is on
193
194         if (( ${4} )); then
195                 cat "${2}" | sed "s/Bundle-Version:.*/Bundle-Version: ${PV}/" > \
196                         "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
197         else
198                 cat "${2}" > "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF"
199         fi
200
201         _java-osgi_plugin "${3}"
202
203         jar cfm "${_OSGI_T}/osgi/${jarName}" "${_OSGI_T}/tmp_jar/META-INF/MANIFEST.MF" \
204                 -C "${_OSGI_T}/tmp_jar/" . > /dev/null || die "Unable to recreate the OSGi compliant jar"
205         rm -rf "${_OSGI_T}/tmp_jar"
206 }
207
208 # @FUNCTION: java-osgi_newjar-fromfile
209 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name> <version rewriting>
210 # @DESCRIPTION:
211 # This function produces an OSGi compliant jar from a given manifest file.
212 # The Manifest Bundle-Version header will be replaced by the current version
213 # of the package, unless the --no-auto-version option is given.
214 # It will call java-pkg_newjar at the end.
215 #
216 # @CODE
217 #       java-osgi_newjar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
218 # @CODE
219 #
220 # @param $opt
221 #       --no-auto-version - This option disables automatic rewriting of the
222 #               version in the Manifest file
223 #
224 # @param $1 - name of jar to repackage with OSGi
225 # @param $2 (optional) - name of the target jar. It will default to package name if not specified.
226 # @param $3 - path to the Manifest file
227 # @param $4 - bundle name
228 java-osgi_newjar-fromfile() {
229         debug-print-function ${FUNCNAME} "$@"
230         local versionRewriting=1
231
232         if [[ "${1}" == "--no-auto-version" ]]; then
233                 versionRewriting=0
234                 shift
235         fi
236         local jarName="$(basename ${1})"
237
238         if (( ${#} > 3 )); then
239                 _java-osgi_makejar-fromfile "${1}" "${3}" "${4}" "${versionRewriting}"
240                 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}" "${2}"
241         else
242                 _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
243                 java-pkg_newjar "${_OSGI_T}/osgi/${jarName}"
244         fi
245 }
246
247 # @FUNCTION: java-osgi_dojar-fromfile()
248 # @USAGE: <jar to repackage with OSGi> <Manifest file> <bundle name>
249 # @DESCRIPTION:
250 # This function produces an OSGi compliant jar from a given manifestfile.
251 # The Manifest Bundle-Version header will be replaced by the current version
252 # of the package, unless the --no-auto-version option is given.
253 # It will call java-pkg_dojar at the end.
254 #
255 # @CODE
256 #       java-osgi_dojar-fromfile "dist/${PN}.jar" "${FILESDIR}/MANIFEST.MF" "Standard Widget Toolkit for GTK 2.0"
257 # @CODE
258 #
259 # @param $opt
260 #       --no-auto-version - This option disables automatic rewriting of the
261 #               version in the Manifest file
262 #
263 # @param $1 - name of jar to repackage with OSGi
264 # @param $2 - path to the Manifest file
265 # @param $3 - bundle name
266 java-osgi_dojar-fromfile() {
267         debug-print-function ${FUNCNAME} "$@"
268         local versionRewriting=1
269
270         if [[ "${1}" == "--no-auto-version" ]]; then
271                 versionRewriting=0
272                 shift
273         fi
274         local jarName="$(basename ${1})"
275
276         _java-osgi_makejar-fromfile "$@" "${versionRewriting}"
277         java-pkg_dojar "${_OSGI_T}/osgi/${jarName}"
278 }