source: branches/gsoc13-tests/src/package1.0/portsrpm.tcl @ 139170

Last change on this file since 139170 was 111323, checked in by marius@…, 7 years ago

Merge from trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.6 KB
Line 
1# et:ts=4
2# portsrpm.tcl
3# $Id: portsrpm.tcl 111323 2013-09-18 23:11:02Z marius@macports.org $
4#
5# Copyright (c) 2007, 2009, 2011, 2013 The MacPorts Project
6# Copyright (c) 2002 - 2003 Apple Inc.
7# All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17# 3. Neither the name of Apple Inc. nor the names of its contributors
18#    may be used to endorse or promote products derived from this software
19#    without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31# POSSIBILITY OF SUCH DAMAGE.
32#
33
34package provide portsrpm 1.0
35package require portutil 1.0
36package require portfetch 1.0
37
38set org.macports.srpm [target_new org.macports.srpm portsrpm::srpm_main]
39target_runtype ${org.macports.srpm} always
40target_provides ${org.macports.srpm} srpm
41target_requires ${org.macports.srpm} checksum
42
43namespace eval portsrpm {
44}
45
46options package.destpath
47
48# Set up defaults
49default srpm.asroot yes
50
51set_ui_prefix
52
53proc portsrpm::srpm_main {args} {
54    global subport version revision UI_PREFIX
55
56    ui_msg "$UI_PREFIX [format [msgcat::mc "Creating SRPM package for %s-%s"] ${subport} ${version}]"
57
58    return [srpm_pkg $subport $version $revision]
59}
60
61proc portsrpm::srpm_pkg {portname portversion portrevision} {
62    global UI_PREFIX package.destpath portdbpath destpath workpath distpath \
63           prefix categories maintainers description long_description \
64           homepage epoch portpath distfiles os.platform os.arch os.version \
65           os.major
66
67    set fetch_urls {}
68    portfetch::checkfiles fetch_urls
69
70    set rpmdestpath ""
71    if {![string equal ${package.destpath} ${workpath}] && ![string equal ${package.destpath} ""]} {
72        set pkgpath ${package.destpath}
73        file mkdir ${pkgpath}/BUILD \
74                   ${pkgpath}/RPMS \
75                   ${pkgpath}/SOURCES \
76                   ${pkgpath}/SPECS \
77                   ${pkgpath}/SRPMS
78        set rpmdestpath "--define '_topdir ${pkgpath}'"
79    }
80
81    foreach dir [list "${prefix}/src/macports/SRPMS" "${prefix}/src/apple/SRPMS" "/usr/src/apple/SRPMS" "/macports/rpms/SRPMS"] {
82        foreach arch {"src" "nosrc"} {
83            set rpmpath "$dir/${portname}-${portversion}-${portrevision}.${arch}.rpm"
84            if {[file readable $rpmpath] && ([file mtime ${rpmpath}] >= [file mtime ${portpath}/Portfile])} {
85                ui_debug "$rpmpath"
86                ui_msg "$UI_PREFIX [format [msgcat::mc "SRPM package for %s version %s is up-to-date"] ${portname} ${portversion}]"
87                return 0
88            }
89        }
90    }
91
92    set specpath ${workpath}/${portname}-port.spec
93    # long_description, description, or homepage may not exist
94    foreach variable {long_description description homepage categories maintainers} {
95        if {![info exists $variable]} {
96            set pkg_$variable ""
97        } else {
98            set pkg_$variable [set $variable]
99        }
100    }
101    set category   [lindex [split $categories " "] 0]
102    set license    "Unknown"
103    set maintainer $maintainers
104
105    set dependencies {}
106    # get deplist
107    set deps [make_dependency_list $portname]
108    set deps [lsort -unique $deps]
109    foreach dep $deps {
110        set name [lindex [split $dep /] 0]
111        set vers [lindex [split $dep /] 1]
112        # don't re-package ourself
113        if {$name != $portname} {
114            lappend dependencies "${name} >= ${vers}"
115        }
116    }
117
118    # true = .src.rpm (with distfiles), false = .nosrc.rpm (without distfiles)
119    set src false
120
121    #set sourcespath ${prefix}/src/macports/SOURCES
122    set sourcespath "`rpm --eval %{_sourcedir}`"
123
124    system "cp -p ${portpath}/Portfile ${sourcespath}/$portname-Portfile"
125    if {[info exists ${portpath}/files]} {
126        system "cd ${portpath} && zip -r -q ${sourcespath}/$portname-files.zip files -x \\*.DS_Store -x files/.svn\\*"
127        set zip $portname-files.zip
128    } else {
129        set zip ""
130    }
131    foreach dist $distfiles {
132        system "cp -p ${distpath}/${dist} ${sourcespath}/${dist}"
133    }
134
135    write_port_spec ${specpath} $portname $portversion $portrevision $pkg_description $pkg_long_description $pkg_homepage $category $license $maintainer $distfiles $fetch_urls $dependencies $epoch $src $zip
136    system "rpmbuild -bs -v --nodeps ${rpmdestpath} ${specpath}"
137
138    return 0
139}
140
141proc portsrpm::make_dependency_list {portname} {
142    set result {}
143    if {[catch {set res [mport_lookup $portname]} error]} {
144                global errorInfo
145                ui_debug "$errorInfo"
146        ui_error "port lookup failed: $error"
147        return 1
148    }
149    foreach {name array} $res {
150        array set portinfo $array
151
152        if {[info exists portinfo(depends_fetch)] || [info exists portinfo(depends_extract)]
153            || [info exists portinfo(depends_build)] || [info exists portinfo(depends_lib)]} {
154            # get the union of depends_fetch, depends_extract, depends_build and depends_lib
155            # xxx: only examines the portfile component of the depspec
156            set depends {}
157            if {[info exists portinfo(depends_fetch)]} { eval "lappend depends $portinfo(depends_fetch)" }
158            if {[info exists portinfo(depends_extract)]} { eval "lappend depends $portinfo(depends_extract)" }
159            if {[info exists portinfo(depends_build)]} { eval "lappend depends $portinfo(depends_build)" }
160            if {[info exists portinfo(depends_lib)]} { eval "lappend depends $portinfo(depends_lib)" }
161
162            foreach depspec $depends {
163                set dep [lindex [split $depspec :] end]
164
165                # xxx: nasty hack
166                if {$dep != "XFree86"} {
167                    eval "lappend result [make_dependency_list $dep]"
168                }
169            }
170        }
171        lappend result $portinfo(name)/$portinfo(version)
172        unset portinfo
173    }
174    ui_debug "dependencies for ${portname}: $result"
175    return $result
176}
177
178proc portsrpm::word_wrap {orig Length} {
179    set pos 0
180    set line ""
181    set text ""
182
183    set words [split $orig]
184    set numWords [llength $words]
185    for {set cnt 0} {$cnt < $numWords} {incr cnt} {
186        set w [lindex $words $cnt]
187        set wLen [string length $w]
188
189        if {($pos+$wLen < $Length)} {
190            # append word to current line
191            if {$pos} {append line " "; incr pos}
192            append line $w
193            incr pos $wLen
194        } else {
195            # line full => write buffer and  begin a new line
196            if {[string length $text]} {append text "\n"}
197            append text $line
198            set line $w
199            set pos $wLen
200        }
201    }
202
203    if {[string length $text]} {append text "\n"}
204    if {[string length $line]} {append text $line}
205    return $text
206}
207
208proc portsrpm::write_port_spec {specfile portname portversion portrevision description long_description homepage category license maintainer distfiles fetch_urls dependencies epoch src zip} {
209    set specfd [open ${specfile} w+]
210    set origportname ${portname}
211    regsub -all -- "\-" $portversion "_" portversion
212    regsub -all -- "\-" $portname "_" portname
213    puts $specfd "\#Spec file generated by MacPorts
214%define distribution MacPorts
215%define vendor MacPorts
216%define packager ${maintainer}
217
218Summary: ${description}
219Name: ${portname}
220Version: ${portversion}
221Release: ${portrevision}
222Group: ${category}
223License: ${license}
224URL: ${homepage}
225BuildRoot: %{_tmppath}/%{name}-%{version}-root
226Source0: ${portname}-Portfile"
227    if {$zip != ""} {
228        puts $specfd "Source1: $zip"
229    }
230    if {[expr ${epoch} != 0]} {
231            puts $specfd "Epoch: ${epoch}"
232    }
233    set first 2
234    set count $first
235    puts $specfd "#distfiles"
236    foreach file ${distfiles} {
237
238        puts -nonewline $specfd "Source${count}: "
239        if {![info exists $fetch_urls]} {
240        foreach {url_var distfile}  ${fetch_urls} {
241            if {[string equal $distfile $file]} {
242                 global portfetch::$url_var master_sites
243                 set site [lindex [set $url_var] 0]
244                 set file [portfetch::assemble_url $site $distfile]
245                 break
246            }
247        }
248        }
249        puts $specfd $file
250        if (!$src) {
251            puts $specfd "NoSource: $count"
252        }
253        incr count
254    }
255    puts $specfd "AutoReq: no"
256    if {[llength ${dependencies}] != 0} {
257        foreach require ${dependencies} {
258            puts $specfd "BuildRequires: [regsub -all -- "\-" $require "_"]"
259        }
260    }
261    set wrap_description [word_wrap ${long_description} 72]
262    if {$zip != ""} {
263        set and "-a 1"
264    } else {
265        set and ""
266    }
267    puts $specfd "
268%description
269$wrap_description
270
271%prep
272%setup -c $and -T
273cp -p %{SOURCE0} Portfile
274#prepare work area
275port fetch
276port checksum
277port extract
278port patch
279
280%build
281port configure
282port build
283
284%install
285rm -rf \$RPM_BUILD_ROOT
286mkdir -p \$RPM_BUILD_ROOT
287port destroot
288port rpm
289
290%clean
291port clean"
292    close $specfd
293}
Note: See TracBrowser for help on using the repository browser.