aboutsummaryrefslogtreecommitdiff
path: root/kde3/am2cmake
blob: d1154ae73c1a98c5f8165c59872771db3391301b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env ruby

$no_kde=false

class InstallTarget
   def initialize
      @files=""
      @location=""
   end
   def addFiles(files)
      @files=@files+files
   end
   def setLocation(location)
      @location=location
   end
   attr_reader :location, :files
end

NoTarget   = 0
Executable = 1
StaticLib  = 2
SharedLib  = 3
Part       = 4
KDEInit    = 5

class BuildTarget
   def initialize(name, type, withStdPrefix=true, install=true, test=false)
      @name=name
      @type=type
      @sources=Array.new
      @uis=Array.new
      @skels=Array.new
      @stubs=Array.new
      @kcfgs=Array.new
      @am=Array.new
      @stdPrefix=withStdPrefix
      @install=install
      @test=test
      @libs=Array.new

     @libs.push("${QT_AND_KDECORE_LIBS}")
   end

   def addSourceFiles(files)
      files.split.each do |currentSource|
         if currentSource =~ /^\S+\.ui$/
            @uis.push(currentSource)
         elsif currentSource =~ /^(\S+)\.skel$/
            @skels.push($1+".h")
         elsif currentSource =~ /^(\S+)\.stub$/
            @stubs.push($1+".h")
         elsif currentSource =~ /^(\S+)\.kcfgc$/
            @kcfgs.push(currentSource)
         else
            @sources.push(currentSource)
         end
      end
   end

   def addLibs(libs)
      lib=""
      libs.split.each do |currentLib|
         lib=case currentLib
         when "$(LIB_KDEUI)" then "kdeui"
#         when "${LIB_KDECORE}" then "${QT_AND_KDECORE_LIBS}"
         when "$(LIB_KIO)" then "kio"
         when "$(LIB_KDEPRINT)" then "kdeprint"
         when "$(LIB_KPARTS)" then "kparts"
         else
            if currentLib =~ /^-l(\S+)$/
               $1
            else
               ""
            end
         end
      end

      if !lib.empty?
         @libs.push(lib)
#         printf("--- lib: #{lib}\n")
      end
   end


   attr_reader :name, :type, :sources, :uis, :skels, :stdPrefix, :stubs, :kcfgs, :install, :test, :libs

end

class CMakeFile
   def initialize(amFile)
      printf("converting #{amFile}\n")
      @amFile=amFile
      amFile =~ /(.*)Makefile.am/
      @path=$1
      @listsFile=@path+"CMakeLists.txt"
      @iconDir="hicolor"
      @installIcons=false

      @targets=Array.new
      @installs=Hash.new

      @includeDirs=Array.new
      @subDirs=Array.new
      @skippedSubDirs=Array.new

      parseFile

      if $no_kde==false
          @includeDirs.push("${KDE3_INCLUDE_DIR}")
          @includeDirs.push("${QT_INCLUDE_DIR}")
      end
      @includeDirs.push("${CMAKE_CURRENT_SOURCE_DIR}")
      @includeDirs.push("${CMAKE_CURRENT_BINARY_DIR}")
   end

   def parseFile
      @lines=IO.readlines(@amFile)
      cummLine=""
      appendNextLine=false
      for line in @lines do
         if line.include?("#")
            line=line[0, line.index("#")]
         end
         if line.length<2
            next
         end

         appendNextLine=(line[line.length-2, 1]=='\\')

         if appendNextLine
            cummLine+=" "+line[0, line.length-2]
         else
            cummLine+=" "+line.chomp #[0, line.length-1]
            if not cummLine.empty?
               evalLine(cummLine)
               cummLine=""
            end
         end
      end
   end

   def findTarget(line)
      type=SharedLib
      if line =~ /^\s*lib(\S+)_la_\S+\s*=\s*\S+.*$/
         targetName=$1
#         type=SharedLib
      elsif line =~ /^\s*(\S+)_la_\S+\s*=\s*\S+.*$/
         targetName=$1
#         type=Executable
      elsif line =~ /^\s*(\S+)_\S+\s*=\s*\S+.*$/
         targetName=$1
#         type=Executable
      end
      @targets.each do |buildTarget|
         amBuildTargetName=buildTarget.name.gsub(/\./, "_")
         if (amBuildTargetName==targetName)
            return buildTarget
         end
      end

      return BuildTarget.new("Dummy", NoTarget)
   end

   def addTarget(line)
      type=NoTarget
      targets=""
      installTarget=true
      testTarget=false

      if line =~ /^\s*lib_LTLIBRARIES\s*=\s*(\S+.*)/
         targets=$1
         type=SharedLib
#         printf("shared: %s\n", $1)
      elsif line =~ /^\s*noinst_LTLIBRARIES\s*=\s*(\S+.*)/
         targets=$1
         type=StaticLib
#         printf("static: %s\n", $1)
      elsif line =~ /^\s*kde_module_LTLIBRARIES\s*=\s*(\S+.*)/
#         printf("part: %s\n", $1)
         targets=$1
         type=Part
      elsif line =~ /^\s*kde_style_LTLIBRARIES\s*=\s*(\S+.*)/
#         printf("style: %s\n", $1)
         targets=$1
         type=Part
      elsif line =~ /^\s*kde_widget_LTLIBRARIES\s*=\s*(\S+.*)/
#         printf("style: %s\n", $1)
         targets=$1
         type=Part
      elsif line =~ /^\s*kdeinit_LTLIBRARIES\s*=\s*(\S+.*)/
#         printf("kdeinitpart: %s\n", $1)
         targets=$1
         type=KDEInit
      elsif line =~ /^\s*bin_PROGRAMS\s*=\s*(\S+.*)$/
         targets=$1
#         printf("exec: %s\n", $1)
         type=Executable
      elsif line =~ /^\s*noinst_PROGRAMS\s*=\s*(\S+.*)$/
         targets=$1
         installTarget=false
#         printf("exec: %s\n", $1)
         type=Executable
      elsif line =~ /^\s*check_PROGRAMS\s*=\s*(\S+.*)$/
         targets=$1
         installTarget=false
         testTarget=true

#         printf("exec: %s\n", $1)
         type=Executable
      elsif line =~ /^\s*EXTRA_PROGRAMS\s*=\s*(\S+.*)$/
         targets=$1
         installTarget=false
         testTarget=true
#         printf("exec: %s\n", $1)
         type=Executable
      else
         return false
      end

      if type==Executable
         targets.split.each{ |current| @targets.push(BuildTarget.new(current, type, true, installTarget, testTarget)) }
      else
         targets.split.each do |current|
            if current =~ /lib(\S+)\.la/
#                printf("adding target with \"lib\": -%s-\n", $1)
               @targets.push(BuildTarget.new($1, type))
             else
               if current =~ /\s*(\S+)\.la/
#                    printf("adding target without \"lib\": -%s-\n", $1)
                  @targets.push(BuildTarget.new($1, type, false))
               end
             end
         end
      end
      return true
   end

   def addSourcesToTarget(line)
#      printf("sources: %s\n", line)
      buildTarget=findTarget(line)
      if buildTarget.type==NoTarget
         printf("PROBLEM: target not found: %s\n", line)
         return
      end

      line =~ /^\s*(lib)?\S+(_la)?_SOURCES\s*=\s*(\S+.*)$/
      buildTarget.addSourceFiles($3)
   end

   def addIncludeDirectories(includeDirs)
      includeDirs.split.each do |dir|
         if dir =~ /^\s*-I\$\(top_srcdir\)(\S+)/
            @includeDirs.push("${CMAKE_SOURCE_DIR}"+$1)
         end
      end
   end

   def addInstallFiles(key, files)
      if @installs.has_key?(key)
         inst=@installs[key]
      else
         inst=InstallTarget.new
      end
      inst.addFiles(files)
      case key
        when "kde_services" then inst.setLocation("/share/services")
        when "kde_kcfg" then inst.setLocation("/share/config.kcfg")
        when "xdg_apps" then inst.setLocation("/share/applications/kde")
      end

      @installs[key]=inst
   end

   def addInstallLocation(key, location)
#      printf("adding loc: %s \n", location)
      if @installs.has_key?(key)
         inst=@installs[key]
      else
         inst=InstallTarget.new
      end

      if location =~ /\$\((\S+)\)(\/?\S*)/
         baseDir=$1
         subDir=$2
#         printf("base: %s sub: %s\n", baseDir, subDir)
         if baseDir=="kde_servicesdir"
            inst.setLocation("/share/services"+subDir)
            @installs[key]=inst
         elsif baseDir=="kde_servicetypesdir"
            inst.setLocation("/share/servicetypes"+subDir)
            @installs[key]=inst
         elsif baseDir=="kde_mimedir"
            inst.setLocation("/share/mimelink"+subDir)
            @installs[key]=inst
         elsif baseDir=="kde_htmldir"
            inst.setLocation("/share/doc/HTML"+subDir)
            @installs[key]=inst
         elsif baseDir=="kde_icondir"
            inst.setLocation("/share/icons"+subDir)
            @installs[key]=inst
            @iconDir=key
         elsif baseDir=="kde_datadir"
            inst.setLocation("/share/apps"+subDir)
            @installs[key]=inst
         elsif baseDir=="datadir"
            inst.setLocation("/share"+subDir)
            @installs[key]=inst
         end
      end
   end

   def evalLine(line)
      if line =~ /^\s*METASOURCES\s*=\s*AUTO\s*$/
         @automoc=true
         return
      end

      if addTarget(line)
         return
      end

      if line =~ /^\s*KDE_ICON\s*=/
         @installIcons=true
         return
      end

      if (line =~ /^\s*\S+_SOURCES\s*=/)
         addSourcesToTarget(line)
         return
      end

      if (line =~ /^\s*(\S+)_LDFLAGS\s*=\s*(\S+.*)$/) ||
         (line =~ /^\s*(\S+)_LIBADD\s*=\s*(\S+.*)$/) ||
         (line =~ /^\s*(\S+)_LDADD\s*=\s*(\S+.*)$/)

         if $1 != "AM"
            buildTarget=findTarget(line)
            if buildTarget.type==NoTarget
               printf("PROBLEM: target %s not found: %s\n", $1, line)
               return
            end

#            printf("target: #{buildTarget.name} lib: #{$2} line: #{line} d1: #{$1}\n")
            buildTarget.addLibs($2)
         end
         return
      end

      if (line =~ /^\s*INCLUDES\s*=\s*(\S+.*)$/)
         addIncludeDirectories($1)
         return
      end

      if line =~ /^\s*(\S+)dir\s*=\s*(\S+.*)$/
         addInstallLocation($1, $2)
         return
      end
      if line =~ /^\s*(\S+)_DATA\s*=\s*(\S+.*)$/
         addInstallFiles($1, $2)
         return
      end

      if line =~ /^\s*SUBDIRS\s*=\s*(\S+.*)$/
         ($1).split.each do |dir|
            if dir =~ /\$\(.+\)/
               @skippedSubDirs.push(dir)
            else
               @subDirs.push(dir) if dir!="."
            end
         end
      end

   end

   def createListsFile
#      prevLines=Array.new
#      if FileTest.exists?(@listsFile)
##         printf("file exists: %s\n", @listsFile)
#         prevLines=File.new(@listsFile, "r").readlines
#      end

      file=File.new(@listsFile, "w+");
      if @amFile=="Makefile.am" && $no_kde==false         # the toplevel Makefile.am
          file.printf("FIND_PACKAGE(KDE3 REQUIRED)\n\n")
          file.printf("SET(CMAKE_VERBOSE_MAKEFILE ON)\n\n")
          file.printf("ADD_DEFINITIONS(${QT_DEFINITIONS} ${KDE3_DEFINITIONS})\n\n")
          file.printf("LINK_DIRECTORIES(${KDE3_LIB_DIR})\n\n")

      end

      if not @subDirs.empty?
         @subDirs.each{ |dir| file.printf("ADD_SUBDIRECTORY( %s ) \n", dir)}
         file.printf("\n")
      end
      if not @skippedSubDirs.empty?
         @skippedSubDirs.each{ |dir| file.printf("MESSAGE(STATUS \"${CMAKE_CURRENT_SOURCE_DIR}: skipped subdir %s\")\n", dir)}
      end


      if not @includeDirs.empty?
         file.printf("INCLUDE_DIRECTORIES( ")
         @includeDirs.each{ |dir| file.printf("%s ", dir) }
         file.printf(" )\n\n")
      end
      @targets.each do |buildTarget|
         file.printf("\n########### next target ###############\n\n")
#         printf("target name: %s\n", buildTarget.name)

         if buildTarget.type==SharedLib
            srcsName=buildTarget.name+"_LIB_SRCS"
         elsif buildTarget.type==StaticLib
            srcsName=buildTarget.name+"_STAT_SRCS"
         elsif buildTarget.type==Part
            srcsName=buildTarget.name+"_PART_SRCS"
         elsif buildTarget.type==KDEInit
            srcsName=buildTarget.name+"_KDEINIT_SRCS"
         else
            srcsName=buildTarget.name+"_SRCS"
         end
         uisName=buildTarget.name+"_UI"
         skelsName=buildTarget.name+"_DCOP_SKEL_SRCS"
         stubsName=buildTarget.name+"_DCOP_STUB_SRCS"
         kcfgsName=buildTarget.name+"_KCFG_SRCS"

         if buildTarget.sources.empty?
            buildTarget.sources.push("dummy.cpp")
         end

         if not buildTarget.sources.empty?
            file.printf("SET(%s\n", srcsName)
            needToCreateDummyFile=false
            buildTarget.sources.each do |currentFile|
               file.printf("%s\n", currentFile)
               if currentFile=="dummy.cpp"

                  needToCreateDummyFile=true if not FileTest.exists?(@path+"/dummy.cpp")
               end
            end
            file.printf(")\n\n")
            file.printf("KDE3_AUTOMOC(${%s})\n\n", srcsName)

            if needToCreateDummyFile
#                  printf("creating dummy file in #{@path} ________\n")
               file.printf("FILE(WRITE dummy.cpp \"//autogenerated file by cmake\\n\")\n")
            end
         end

         if not buildTarget.uis.empty?
            file.printf("SET( %s\n", uisName)
            buildTarget.uis.each{ |currentFile| file.printf("%s\n", currentFile)}
            file.printf(")\n\n")
            file.printf("KDE3_ADD_UI_FILES(%s ${%s} )\n\n", srcsName, uisName)
         end

         if not buildTarget.skels.empty?
            file.printf("SET( %s\n", skelsName)
            buildTarget.skels.each{ |currentFile| file.printf("%s\n", currentFile)}
            file.printf(")\n\n")

            file.printf("KDE3_ADD_DCOP_SKELS(%s ${%s})\n\n", srcsName, skelsName)
         end

         if not buildTarget.stubs.empty?
            file.printf("SET( %s\n", stubsName)
            buildTarget.stubs.each{ |currentFile| file.printf("%s\n", currentFile)}
            file.printf(")\n\n")

            file.printf("KDE3_ADD_DCOP_STUBS(%s ${%s})\n\n", srcsName, stubsName)
         end

         if not buildTarget.kcfgs.empty?
            file.printf("SET( %s\n", kcfgsName)
            buildTarget.kcfgs.each{ |currentFile| file.printf("%s\n", currentFile)}
            file.printf(")\n\n")

            file.printf("KDE3_ADD_KCFG_FILES(%s ${%s})\n\n", srcsName, kcfgsName)
         end

         if buildTarget.type==SharedLib
            file.printf("ADD_LIBRARY(%s SHARED ${%s})\n\n", buildTarget.name, srcsName)
            file.printf("TARGET_LINK_LIBRARIES(%s ", buildTarget.name)
            buildTarget.libs.each { |currentLib| file.printf(" %s", currentLib) }
            file.printf(" )\n\n")

            file.printf("SET_TARGET_PROPERTIES(%s PROPERTIES VERSION 4.2.0 SOVERSION 4 )\n", buildTarget.name)
            file.printf("INSTALL_TARGETS(/lib %s )\n\n", buildTarget.name)
         elsif buildTarget.type==StaticLib
            file.printf("ADD_LIBRARY(%s STATIC ${%s})\n\n", buildTarget.name, srcsName)
         elsif buildTarget.type==Part
            if buildTarget.stdPrefix
               file.printf("KDE3_ADD_KPART(%s WITH_PREFIX ${%s})\n\n", buildTarget.name, srcsName)
            else
               file.printf("KDE3_ADD_KPART(%s ${%s})\n\n", buildTarget.name, srcsName)
            end
            file.printf("TARGET_LINK_LIBRARIES(%s ", buildTarget.name)
            buildTarget.libs.each { |currentLib| file.printf(" %s", currentLib) }
            file.printf(" )\n\n")

            file.printf("INSTALL_TARGETS(/lib/kde3 %s )\n\n", buildTarget.name)
         elsif buildTarget.type==KDEInit
            file.printf("KDE3_ADD_KLM( %s ${%s})\n\n", buildTarget.name, srcsName)

            file.printf("TARGET_LINK_LIBRARIES(kdeinit_%s ", buildTarget.name)
            buildTarget.libs.each { |currentLib| file.printf(" %s", currentLib) }
            file.printf(" )\n\n")

            file.printf("INSTALL_TARGETS(/lib kdeinit_%s )\n\n", buildTarget.name)

            file.printf("TARGET_LINK_LIBRARIES( %s kdeinit_%s )\n", buildTarget.name, buildTarget.name)

            file.printf("INSTALL_TARGETS(/bin %s )\n", buildTarget.name)

         else  #executable
            if $no_kde

               file.printf("ADD_EXECUTABLE(%s ${%s})\n\n", buildTarget.name, srcsName)

               file.printf("TARGET_LINK_LIBRARIES(%s ", buildTarget.name)
               buildTarget.libs.each { |currentLib| file.printf(" %s", currentLib) }
               file.printf(" )\n\n")

               if buildTarget.install
                  file.printf("INSTALL_TARGETS(/bin %s )\n\n", buildTarget.name)
               end

            else
               if buildTarget.test
                  file.printf("IF(KDE3_BUILD_TESTS)\n")
               end

               file.printf("KDE3_ADD_EXECUTABLE(%s ${%s})\n\n", buildTarget.name, srcsName)

               file.printf("TARGET_LINK_LIBRARIES(%s ", buildTarget.name)
               buildTarget.libs.each { |currentLib| file.printf(" %s", currentLib) }
               file.printf(" )\n\n")

               if buildTarget.install
                  file.printf("INSTALL_TARGETS(/bin %s )\n\n", buildTarget.name)
               end

               if buildTarget.test
                  file.printf("ENDIF(KDE3_BUILD_TESTS)\n")
               end

            end

         end

      end

      file.printf("\n########### install files ###############\n\n")

      @installs.each do |key, install|
         file.printf("INSTALL_FILES( %s FILES %s )\n", install.location, install.files)
      end
      file.printf("\n")

      if @installIcons
         file.printf("KDE3_INSTALL_ICONS( %s )\n\n",@iconDir )
      end

      file.printf("KDE3_PLACEHOLDER()\n\n")

      file.printf("\n\n#original Makefile.am contents follow:\n\n")
      @lines.each{ |line| file.printf("#%s", line)}
#      if !prevLines.empty?
#         file.printf("\n#######################\n#previous CMakeLists.txt contents follow:\n\n")
#         prevLines.each{ |line| file.printf("#%s", line)}
#      end
   end
end

def convertAmFile(amFile)
   cmake=CMakeFile.new(amFile)
   cmake.createListsFile
end

if (ARGV.length==1)
   if ARGV[0]=="--no-kde"
      $no_kde=true
   elsif ARGV[0]=="--help"
      printf("--help\t print this help text\n")
      printf("--version\t print version information\n")
      printf("--no-kde\t disable special KDE application support\n")
   elsif ARGV[0]=="--version"
      printf("am2cmake (C) 2005, Alexander Neundorf\n")
      printf("Version 0.1, December 11th, 2005\n");
   else
      printf("Invalid argument, try --help\n")
   end
end
Dir["**/Makefile.am"].each{ |currentFile| convertAmFile(currentFile)}