aboutsummaryrefslogtreecommitdiff
path: root/am2cmake
blob: f37ddc98b0d78d7d83e81b0f70fc156be5d46d98 (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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
#!/usr/bin/env ruby

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

BuildNoKDE=0
BuildKDE3=1
BuildKDE4=2

$withConv=false


$buildType=BuildKDE3

$allTargets=Array.new

InstallDirsKDE3 = {
   "kde_apps"     => "/share/applnk",
   "kde_conf"     => "/share/config",
   "kde_data"     => "/share/apps",
   "kde_html"     => "/share/doc/HTML",
   "kde_icon"     => "/share/icons",
   "kde_kcfg"     => "/share/config.kcfg",
   "kde_libs_html"=> "/share/doc/HTML",
   "kde_locale"   => "/share/locale",
   "kde_mime"     => "/share/mimelink",
   "kde_services" => "/share/services",
   "kde_servicetypes" => "/share/servicetypes",
   "kde_sound"    => "/share/sounds",
   "kde_templates"=> "/share/templates",
   "kde_wallpaper"=> "/share/wallpapers",
   "xdg_apps"     => "/share/applications/kde",
   "xdg_directory"=> "/share/desktop-directories",
   "data"         => "/share",
   "include"      => "/include"
}

InstallDirsKDE4 = {
   "kde_apps"     => "${APPLNK_INSTALL_DIR}",
   "kde_conf"     => "${CONFIG_INSTALL_DIR}",
   "kde_data"     => "${DATA_INSTALL_DIR}",
   "kde_html"     => "${HTML_INSTALL_DIR}",
   "kde_icon"     => "${KDE4_ICON_DIR}",
   "kde_kcfg"     => "${KCFG_INSTALL_DIR}",
   "kde_libs_html"=> "${LIBS_HTML_INSTALL_DIR}",
   "kde_locale"   => "${LOCALE_INSTALL_DIR}",
   "kde_mime"     => "${MIME_INSTALL_DIR}",
   "kde_services" => "${SERVICES_INSTALL_DIR}",
   "kde_servicetypes" => "${SERVICETYPES_INSTALL_DIR}",
   "kde_sound"    => "${SOUND_INSTALL_DIR}",
   "kde_templates"=> "${TEMPLATES_INSTALL_DIR}",
   "kde_wallpaper"=> "${WALLPAPER_INSTALL_DIR}",
   "xdg_apps"     => "${XDG_APPS_DIR}",
   "xdg_directory"=> "${XDG_DIRECTORY_DIR}",
   "data"         => "/share",
   "include"      => "/include"
}

$installDirs=InstallDirsKDE3

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

class BuildTarget
   def initialize(name, type, withStdPrefix=true, install=true, test=false)
      @name=name
      @type=type
      @sources=Array.new
      @ui3s=Array.new # for ui3 files in a kde4 build
      @uis=Array.new
      @skels=Array.new
      @stubs=Array.new
      @kcfgs=Array.new
      @qrcs=Array.new
      @am=Array.new
      @stdPrefix=withStdPrefix
      @install=install
      @test=test
      @libs=Array.new
      @versionNumberMajor=1
      @versionNumberMinor=0
      @versionNumberPatch=0

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

   def addSourceFiles(files)
      files.split.each do |currentSource|
         if currentSource =~ /^\S+\.ui$/
            @uis.push(currentSource)
         elsif currentSource =~ /^(\S+)\.ui4$/
            @uis.push(currentSource)
         elsif currentSource =~ /^(\S+)\.ui3$/  #Qt3 ui file in a KDE 4 build
            @ui3s.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)
         elsif currentSource =~ /^(\S+)\.qrc$/
            @qrcs.push(currentSource)
         else
            @sources.push(currentSource)
         end
      end
   end

   def addLibs(libs)
      lib=""
      previousWasVersionInfo=false
      libs.split.each do |currentLib|
         lib=case currentLib
         when "$(LIB_QT3SUPPORT)" then "${QT_QT3SUPPORT_LIBRARY}"
         when "$(DCOP_LIB)" then "DCOP"
         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"
		 when "$(LIB_KSPELL2)" then "kspell2"
		 when "$(LIB_KDE3SUPPORT)" then "kde3support"
		 when "$(LIB_KJS)" then "kjs"
		 when "$(LIB_KHTML)" then "khtml"
		 when "$(LIB_KNEWSTUFF)" then "knewstuff"
		 when "$(LIB_KDEPIM)" then "kdepim"
		 when "$(LIB_KDNSSD)" then "kdnssd"
		 when "$(LIB_KIMPROXY)" then "kimproxy"
		 when "$(LIB_KUTILS)" then "kutils"
		 when "$(LIB_KSYCOCA)" then "kio"
         else
            if currentLib =~ /^-l(\S+)$/
               $1
            elsif currentLib =~ /^(.+\/)?lib(\w+)\.la$/
               $2
            else
               ""
            end
         end

         if !lib.empty?
            @libs.push(lib)
         end

         #handle libtool version info, not sure it is correct
         if previousWasVersionInfo
            if currentLib =~ /^(\d+):(\d+):(\d+)$/
               @versionNumberMajor=$1.to_i-$3.to_i
               @versionNumberMinor=$3.to_i
               @versionNumberPatch=$2.to_i
            elsif previousWasVersionInfo && currentLib =~ /^(\d+):(\d+)$/
               @versionNumberMajor=$1.to_i
               @versionNumberMinor=$2.to_i
               @versionNumberPatch=0
            end
         end
         previousWasVersionInfo= (currentLib == "-version-info")

      end

   end


   attr_reader :name, :type, :sources, :uis, :ui3s, :skels, :stdPrefix, :stubs, :kcfgs, :install, :test, :libs, :qrcs
   attr_reader :versionNumberMajor, :versionNumberMinor, :versionNumberPatch

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

      @createDoxygenDocs=false
      @doxygenRecursive=false
      @doxygenInternalDocs=false
      @doxygenReferences=Array.new
      @doxygenExcludes=Array.new

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

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

      @configHeaders=Array.new
      $configHeaders.each do  |header|
         @configHeaders.push($1) if header=~ Regexp.new("^"+@path+"([^\\/]+\\.h)\\.in")
      end

      parseFile

      if $buildType==BuildKDE3
          @includeDirs.push("${KDE3_INCLUDE_DIR}")
          @includeDirs.push("${QT_INCLUDE_DIR}")
      elsif $buildType==BuildKDE4
          @includeDirs.push("${KDE4_INCLUDE_DIR}")
          @includeDirs.push("${QT_INCLUDES}")
      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?
               parseLine(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
         $stderr.printf("%s PROBLEM: target not found: %s\n", @amFile, 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)
      if $installDirs.has_key?(key)
         inst.setLocation($installDirs[key])
      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+)dir\)(\/?\S*)/
         baseDir=$1
         subDir=$2
         if $installDirs.has_key?(baseDir)
            inst.setLocation($installDirs[baseDir]+subDir)
            @installs[key]=inst
         end
         if baseDir=="kde_icon"
            @iconDir=key
         end
      end
   end

   def parseDoxygenSettings(line)
      if line.include?("Doxyfile.am")
         @createDoxygenDocs=true
      elsif line =~ /^\s*DOXYGEN_SET_INTERNAL_DOCS\s*=\sYES.*/
         @doxygenInternalDocs = true
      elsif line =~ /^\s*DOXYGEN_SET_RECURSIVE\s*=\sYES.*/
         @doxygenRecursive = true
      elsif line =~ /^\s*DOXYGEN_REFERENCES\s*=\s*(\S+.*)$/
         ($1).split.each { |ref| @doxygenReferences.push(ref) }
      elsif line =~ /^\s*DOXYGEN_EXCLUDE\s*=\s*(\S+.*)$/
         ($1).split.each { |exclude| @doxygenExcludes.push(exclude) }
      end
   end

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

      if addTarget(line)
         return
      end


      if line.include?("Doxyfile.am") || line.include?("DOXYGEN")
         parseDoxygenSettings(line)
      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
               $stderr.printf("%s PROBLEM: target %s not found: %s\n", @amFile, $1, line)
               return
            end

#            $stderr.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*(\w*include)_HEADERS\s*=\s*(\S+.*)$/
         addInstallFiles($1, $2)
         return
      end

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

   def createKDE3ListsFile

      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 !@configHeaders.empty?
         @configHeaders.each{ |header| file.printf("CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/#{header}.cmake ${CMAKE_CURRENT_BINARY_DIR}/#{header} )\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\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)}
   end


   def createKDE4ListsFile
      file=File.new(@listsFile, "w+");
      file.printf("kde4_header()\n\n")

      if @amFile=="Makefile.am"          # the toplevel Makefile.am
          file.printf("find_package(KDE4 REQUIRED)\n\n")
          file.printf("set(CMAKE_VERBOSE_MAKEFILE ON)\n\n")
          file.printf("add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})\n\n")
          file.printf("link_directories(${KDE4_LIB_DIR})\n\n")
          file.printf("include(ConvenienceLibs.cmake)\n\n")
		  file.printf("include(ManualStuff.cmake)\n\n")
          file.printf("include(ConfigureChecks.cmake)\n\n")
      end

      if !@configHeaders.empty?
         @configHeaders.each{ |header| file.printf("configure_file(${CMAKE_CURRENT_SOURCE_DIR}/#{header}.cmake ${CMAKE_CURRENT_BINARY_DIR}/#{header} )\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"
#</porting info for libtool convenience libs>
         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"
         ui3sName=buildTarget.name+"_UI3"
         skelsName=buildTarget.name+"_DCOP_SKEL_SRCS"
         stubsName=buildTarget.name+"_DCOP_STUB_SRCS"
         kcfgsName=buildTarget.name+"_KCFG_SRCS"
         qrcsName=buildTarget.name+"_QRC"


         if buildTarget.type==StaticLib && $withConvLibs
#<porting info for libtool convenience libs>
            $convFile.printf("# %s: %s\n\n", @amFile, buildTarget.name)

            if buildTarget.sources.empty?
               $convFile.printf("set(%s \n", srcsName)
               buildTarget.sources.each { |currentFile| $convFile.printf("  ${CMAKE_SOURCE_DIR}/%s%s\n", @path, currentFile) }
               $convFile.printf(")\n\n", srcsName)
            end
            if not buildTarget.uis.empty?
               $convFile.printf("set( %s\n", uisName)
               buildTarget.uis.each{ |currentFile| $convFile.printf("%s\n", currentFile)}
               $convFile.printf(")\n\n")
            end
            if not buildTarget.ui3s.empty?
               $convFile.printf("set( %s\n", ui3sName)
               buildTarget.ui3s.each{ |currentFile| $convFile.printf("%s\n", currentFile)}
               $convFile.printf(")\n\n")
            end
            if not buildTarget.qrcs.empty?
               $convFile.printf("set( %s\n", qrcsName)
               buildTarget.qrcs.each{ |currentFile| $convFile.printf("%s\n", currentFile)}
               $convFile.printf(")\n\n")
            end
            if not buildTarget.skels.empty?
               $convFile.printf("set( %s\n", skelsName)
               buildTarget.skels.each{ |currentFile| $convFile.printf("%s\n", currentFile)}
               $convFile.printf(")\n\n")
            end
            if not buildTarget.stubs.empty?
               $convFile.printf("set( %s\n", stubsName)
               buildTarget.stubs.each{ |currentFile| $convFile.printf("%s\n", currentFile)}
               $convFile.printf(")\n\n")
            end
            if not buildTarget.kcfgs.empty?
               $convFile.printf("set( %s\n", kcfgsName)
               buildTarget.kcfgs.each{ |currentFile| $convFile.printf("%s\n", currentFile)}
               $convFile.printf(")\n\n")
            end
            next # do nothing else for static libs
         end

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

         if not buildTarget.sources.empty?
            file.printf("set(%s ", srcsName)
            needToCreateDummyFile=false
            buildTarget.sources.each do |currentFile|
               # if there are more than 3 files, print each of them on its own line
               file.printf("\n   ") if buildTarget.sources.size>3
               file.printf("%s ", currentFile)
               if currentFile=="dummy.cpp"

                  needToCreateDummyFile=true if not FileTest.exists?(@path+"/dummy.cpp")
               end
            end
            file.printf(")\n\n")
            file.printf("kde4_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("kde4_add_ui_files(%s ", srcsName )
            buildTarget.uis.each do |currentFile| 
               # if there are more than 3 files, print each of them on its own line
               file.printf("\n   ") if buildTarget.uis.size>3
               file.printf("%s ", currentFile)
            end
            file.printf(")\n\n")
         end

         if not buildTarget.ui3s.empty?
            file.printf("kde4_add_ui3_files(%s ", srcsName)
            buildTarget.ui3s.each do |currentFile| 
               # if there are more than 3 files, print each of them on its own line
               file.printf("\n   ") if buildTarget.ui3s.size>3
               file.printf("%s ", currentFile)
            end
            file.printf(")\n\n")
         end

         if not buildTarget.qrcs.empty?
            file.printf("qt4_add_resources(%s ", srcsName)
            buildTarget.qrcs.each do |currentFile| 
               # if there are more than 3 files, print each of them on its own line
               file.printf("\n   ") if buildTarget.qrcs.size>3
               file.printf("%s ", currentFile)
            end
            file.printf(")\n\n")
         end

         if not buildTarget.skels.empty?
            file.printf("kde4_add_dcop_skels(%s ", srcsName)
            buildTarget.skels.each do |currentFile| 
               # if there are more than 3 files, print each of them on its own line
               file.printf("\n   ") if buildTarget.skels.size>3
               file.printf("%s ", currentFile)
            end
            file.printf(")\n\n")
         end

         if not buildTarget.stubs.empty?
            file.printf("kde4_add_dcop_stubs(%s ", srcsName)
            buildTarget.stubs.each do |currentFile| 
               # if there are more than 3 files, print each of them on its own line
               file.printf("\n   ") if buildTarget.stubs.size>3
               file.printf("%s ", currentFile)
            end
            file.printf(")\n\n")
         end

         if not buildTarget.kcfgs.empty?
            file.printf("kde4_add_kcfg_files(%s ", srcsName)
            buildTarget.kcfgs.each do |currentFile| 
               # if there are more than 3 files, print each of them on its own line
               file.printf("\n   ") if buildTarget.kcfgs.size>3
               file.printf("%s ", currentFile)
            end
            file.printf(")\n\n")
         end

         if buildTarget.type==SharedLib
            file.printf("kde4_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 %d.%d.%d SOVERSION %d )\n", buildTarget.name, buildTarget.versionNumberMajor, buildTarget.versionNumberMinor, buildTarget.versionNumberPatch, buildTarget.versionNumberMajor)
            file.printf("install_targets(${LIB_INSTALL_DIR} %s )\n\n", buildTarget.name)

         elsif buildTarget.type==StaticLib
            file.printf("kde4_add_library(%s STATIC ${%s})\n\n", buildTarget.name, srcsName)

         elsif buildTarget.type==Part
            if buildTarget.stdPrefix
               file.printf("kde4_add_plugin(%s WITH_PREFIX ${%s})\n\n", buildTarget.name, srcsName)
               file.printf("kde4_install_libtool_file( ${PLUGIN_INSTALL_DIR} %s )\n\n", buildTarget.name)
            else
               file.printf("kde4_add_plugin(%s ${%s})\n\n", buildTarget.name, srcsName)
               file.printf("kde4_install_libtool_file( ${PLUGIN_INSTALL_DIR} %s )\n\n", buildTarget.name)
            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_INSTALL_DIR}/kde4 %s )\n\n", buildTarget.name)
         elsif buildTarget.type==KDEInit
            file.printf("kde4_add_kdeinit_executable( %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_INSTALL_DIR} 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 buildTarget.test
               file.printf("if(KDE4_BUILD_TESTS)\n\n")
            end

            file.printf("kde4_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(KDE4_BUILD_TESTS)\n")
            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
         #add a default theme (not perfect for convert but enough to avoid pb
		 file.printf("kde4_install_icons( %s crystalsvg )\n\n",@iconDir )
      end

      if @createDoxygenDocs
         file.printf("kde4_create_doxygen_docs( ")

         if @doxygenRecursive
            file.printf("RECURSIVE ")
         end

         if @doxygenInternalDocs
            file.printf("INTERNAL_DOCS ")
         end

         if not @doxygenReferences.empty?
            file.printf("REFERENCES ")
            @doxygenReferences.each { |ref| file.printf("%s ", ref) }
         end
         if not @doxygenExcludes.empty?
            file.printf("EXCLUDE ")
            @doxygenExcludes.each { |exclude| file.printf("%s ", exclude) }
         end

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

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

      file.printf("\n\n#original Makefile.am contents follow:\n\n")
      @lines.each{ |line| file.printf("#%s", line)}

   end
end

def convertAmFile(amFile)
   cmake=CMakeFile.new(amFile)
   if $buildType==BuildKDE4
      cmake.createKDE4ListsFile
   else
      cmake.createKDE3ListsFile
   end
end

if (ARGV.length==1)
   if ARGV[0]=="--no-kde"
      $buildType=BuildNoKDE
   elsif ARGV[0]=="--kde3"
      $buildType=BuildKDE3
   elsif ARGV[0]=="--kde4"
      $buildType=BuildKDE4
      $withConvLibs=true
      $installDirs=InstallDirsKDE4
   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")
      printf("--kde3\t create cmake files from KDE 3 automake files\n")
      printf("--kde4\t create cmake files from KDE 4 unsermake files\n")
   elsif ARGV[0]=="--version"
      printf("am2cmake (C) 2005, Alexander Neundorf\n")
      printf("Version 0.2, January 13th, 2006\n");
   else
      printf("Invalid argument, try --help\n")
   end
end

if $withConvLibs
   $convFile=File.new("ConvenienceLibs.cmake", "w+")
   $convFile.printf("\n#former libtool convenience libraries:\n\n")
end


$configHeaders=Dir["**/*.h.in"]
$configInIns=Dir["**/*.in.in"]

infoFile=File.new("AdditionalInfo.txt", "w+")
infoFile.printf(".h.in-Files\n")
$configHeaders.each{|inFile| infoFile.printf("%s\n", inFile)}
infoFile.printf("\n.in.in-Files\n")
$configInIns.each{|inFile| infoFile.printf("%s\n", inFile)}


Dir["**/Makefile.am"].each{ |currentFile| convertAmFile(currentFile)}