summaryrefslogtreecommitdiff
path: root/cmake/helpers/EflMacros.cmake
blob: 7aad512ac57747fdf4ddfea89c35fc19e453287b (plain) (blame)
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
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# NOTE:
#
# - Use function() whenever possible, macro() doesn't start a new
#   variable scope and needs careful handling to avoid left overs.
#
# - To 'export' values or lists, use the macros SET_GLOBAL() and
#   LIST_APPEND_GLOBAL().
#

# LIST_APPEND_GLOBAL(Var Element)
#
# Append to a list in the global variable scope (cache internal)
function(LIST_APPEND_GLOBAL _var _element)
  set(_local ${${_var}})
  list(APPEND _local ${_element})
  set(${_var} ${_local} CACHE INTERNAL "")
endfunction()

# SET_GLOBAL(Var Value [Help])
#
# Set a variable in the global variable scope (cache internal)
function(SET_GLOBAL _var _value)
  set(${_var} "${_value}" CACHE INTERNAL "${ARGN}")
endfunction()

unset(EFL_ALL_OPTIONS CACHE)
unset(EFL_ALL_LIBS CACHE)
unset(EFL_ALL_TESTS CACHE)
unset(EFL_PKG_CONFIG_MISSING_OPTIONAL CACHE)

# EFL_OPTION(Name Help Default)
#
# Declare an option() that will be automatically printed by
# EFL_OPTIONS_SUMMARY()
#
# To extend the EFL_OPTIONS_SUMMARY() message, use
# EFL_OPTION_SET_MESSAGE(Name Message)
function(EFL_OPTION _name _help _defval)
  set(_type)
  set(_vartype)
  set(_choices)
  list(LENGTH ARGN _argc)
  if(_argc LESS 1)
    set(_type BOOL)
    set(_vartype BOOL)
  else()
    list(GET ARGN 0 _type)
    set(_vartype ${_type})
    list(REMOVE_AT ARGN 0)
  endif()
  if(${_vartype} STREQUAL "CHOICE")
    set(_type STRING)
    SET_GLOBAL(EFL_OPTION_CHOICES_${_name} "${ARGN}" "Possible values for ${_name}")
    set(_choices " (Choices: ${ARGN})")
  endif()

  LIST_APPEND_GLOBAL(EFL_ALL_OPTIONS ${_name})

  SET_GLOBAL(EFL_OPTION_DEFAULT_${_name} "${_defval}" "Default value for ${_name}")
  SET_GLOBAL(EFL_OPTION_TYPE_${_name} "${_vartype}" "Type of ${_name}")
  set(${_name} ${_defval} CACHE ${_type} "${_help}${_choices}")
  option(${_name} "${_help}${_choices}" ${_defval})

  if(_choices)
    list(FIND ARGN "${${_name}}" _ret)
    if(${_ret} EQUAL -1)
      message(FATAL_ERROR "Invalid choice ${_name}=${${_name}}${_choices}")
    endif()
  endif()
endfunction()

# EFL_OPTION_SET_MESSAGE(Name Message)
#
# Extends the summary line output by EFL_OPTIONS_SUMMARY()
# with more details.
function(EFL_OPTION_SET_MESSAGE _name _message)
  SET_GLOBAL(EFL_OPTION_MESSAGE_${_name} "${_message}")
endfunction()

# EFL_OPTIONS_SUMMARY()
# Shows the summary of options, their values and related messages.
function(EFL_OPTIONS_SUMMARY)
  message(STATUS "EFL Options Summary:")
  message(STATUS "  CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}")
  message(STATUS "  CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
  foreach(_o ${EFL_ALL_OPTIONS})
    set(_v ${${_o}})
    set(_d ${EFL_OPTION_DEFAULT_${_o}})
    if("${_v}" STREQUAL "${_d}")
      set(_i "default")
    else()
      set(_i "default: ${_d}")
    endif()
    if(EFL_OPTION_MESSAGE_${_o})
      set(_m " [${EFL_OPTION_MESSAGE_${_o}}]")
    else()
      set(_m)
    endif()
    message(STATUS "  ${_o}=${_v} (${_i})${_m}")
  endforeach()
  message(STATUS "EFL Libraries:")
  foreach(_o ${EFL_ALL_LIBS})
    message(STATUS "  ${_o}${_mods}")
    foreach(_m ${${_o}_MODULES})
      string(REGEX REPLACE "^${_o}-module-" "" _m ${_m})
      message(STATUS "    dynamic: ${_m}")
    endforeach()
    foreach(_m ${${_o}_STATIC_MODULES})
      string(REGEX REPLACE "^${_o}-module-" "" _m ${_m})
      message(STATUS "    static.: ${_m}")
    endforeach()
    unset(_m)
  endforeach()

  if(EFL_PKG_CONFIG_MISSING_OPTIONAL)
    message(STATUS "The following pkg-config optional modules are missing:")
    foreach(_m ${EFL_PKG_CONFIG_MISSING_OPTIONAL})
      message(STATUS "  ${_m}")
    endforeach()
    unset(_m)
  endif()
endfunction()

set(EFL_ALL_LIBS)
set(EFL_ALL_TESTS)

# EFL_FINALIZE()
#
# Finalize EFL processing, adding extra targets.
function(EFL_FINALIZE)
  add_custom_target(all-libs DEPENDS ${EFL_ALL_LIBS})
  add_custom_target(all-tests DEPENDS ${EFL_ALL_TESTS})
endfunction()

unset(HEADER_FILE_CONTENT CACHE)

# HEADER_CHECK(header [NAME variable] [INCLUDE_FILES extra1.h .. extraN.h])
#
# Check if the header file exists, in such case define variable
# in configuration file.
#
# Variable defaults to HAVE_${HEADER}, where HEADER is the uppercase
# representation of the first parameter. It can be overridden using
# NAME keyword.
#
# To include extra files, then use INCLUDE_FILES keyword.
function(HEADER_CHECK header)
  string(TOUPPER HAVE_${header} var)
  string(REGEX REPLACE "[^a-zA-Z0-9]" "_" var "${var}")
  string(REGEX REPLACE "_{2,}" "_" var "${var}")

  cmake_parse_arguments(PARAMS "" "NAME" "INCLUDE_FILES" ${ARGN})

  if(PARAMS_NAME)
    set(var ${PARAMS_NAME})
  endif()

  set(CMAKE_EXTRA_INCLUDE_FILES "${PARAMS_INCLUDE_FILES}")

  CHECK_INCLUDE_FILE(${header} ${var})

  if(${${var}})
    SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n")
  else()
    SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n")
  endif()
endfunction()

# FUNC_CHECK(func [NAME variable]
#            [INCLUDE_FILES header1.h .. headerN.h]
#            [LIBRARIES lib1 ... libN]
#            [DEFINITIONS -DA=1 .. -DN=123]
#            [FLAGS -cmdlineparam1 .. -cmdlineparamN]
#            [CXX])
#
# Check if the function exists, in such case define variable in
# configuration file.
#
# Variable defaults to HAVE_${FUNC}, where FUNC is the uppercase
# representation of the first parameter. It can be overridden using
# NAME keyword.
#
# To define include files use INCLUDE_FILES keyword.
#
# To use C++ compiler, use CXX keyword
function(FUNC_CHECK func)
  string(TOUPPER HAVE_${func} var)
  string(REGEX REPLACE "_{2,}" "_" var "${var}")

  cmake_parse_arguments(PARAMS "CXX" "NAME" "INCLUDE_FILES;LIBRARIES;DEFINITIONS;FLAGS" ${ARGN})

  set(CMAKE_REQUIRED_LIBRARIES "${PARAMS_LIBRARIES}")
  set(CMAKE_REQUIRED_DEFINITIONS "${PARAMS_DEFINITIONS}")
  set(CMAKE_REQUIRED_FLAGS "${PARAMS_FLAGS}")

  if(PARAMS_CXX)
    check_cxx_symbol_exists(${func} "${PARAMS_INCLUDE_FILES}" ${var})
  else()
    check_symbol_exists(${func} "${PARAMS_INCLUDE_FILES}" ${var})
  endif()

  if(${${var}} )
    SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n")
  else()
    SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n")
  endif()
endfunction()

# TYPE_CHECK(type [NAME variable]
#           [INCLUDE_FILES file1.h ... fileN.h]
#           [LIBRARIES lib1 ... libN]
#           [DEFINITIONS -DA=1 .. -DN=123]
#           [FLAGS -cmdlineparam1 .. -cmdlineparamN]
#           [CXX])
#
# Check if the type exists and its size, in such case define variable
# in configuration file.
#
# Variable defaults to HAVE_${TYPE}, where TYPE is the uppercase
# representation of the first parameter. It can be overridden using
# NAME keyword.
#
# To define include files use INCLUDE_FILES keyword.
#
# To use C++ compiler, use CXX keyword
function(TYPE_CHECK type)
  string(TOUPPER HAVE_${type} var)
  string(REGEX REPLACE "_{2,}" "_" var "${var}")

  cmake_parse_arguments(PARAMS "CXX" "NAME" "INCLUDE_FILES;LIBRARIES;DEFINITIONS;FLAGS" ${ARGN})

  set(CMAKE_REQUIRED_LIBRARIES "${PARAMS_LIBRARIES}")
  set(CMAKE_REQUIRED_DEFINITIONS "${PARAMS_DEFINITIONS}")
  set(CMAKE_REQUIRED_FLAGS "${PARAMS_FLAGS}")
  set(CMAKE_EXTRA_INCLUDE_FILES "${PARAMS_INCLUDE_FILES}")

  if(PARAMS_CXX)
    set(lang CXX)
  else()
    set(lang C)
  endif()

  CHECK_TYPE_SIZE(${type} ${var} LANGUAGE ${lang})

  if(HAVE_${var})
    SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#define ${var} 1\n")
  else()
    SET_GLOBAL(HEADER_FILE_CONTENT "${HEADER_FILE_CONTENT}#undef ${var}\n")
  endif()
endfunction()

# EFL_HEADER_CHECKS_FINALIZE(file)
#
# Write the configuration gathered with HEADER_CHECK(), TYPE_CHECK()
# and FUNC_CHECK() to the given file.
function(EFL_HEADER_CHECKS_FINALIZE file)
  file(WRITE ${file}.new ${HEADER_FILE_CONTENT})
  if (NOT EXISTS ${file})
    file(RENAME ${file}.new ${file})
    message(STATUS "${file} was generated.")
  else()
    file(MD5 ${file}.new _new_md5)
    file(MD5 ${file} _old_md5)
    if(_new_md5 STREQUAL _old_md5)
      message(STATUS "${file} is unchanged.")
    else()
      file(REMOVE ${file})
      file(RENAME ${file}.new ${file})
      message(STATUS "${file} was updated.")
    endif()
  endif()
  unset(HEADER_FILE_CONTENT CACHE) # allow to reuse with an empty contents
endfunction()

# EFL_FILES_TO_ABSOLUTE(Var Source_Dir Binary_Dir [file1 ... fileN])
#
# Convert list of files to absolute path. If not absolute, then
# check inside Source_Dir and if it fails assumes it's inside Binary_Dir
function(EFL_FILES_TO_ABSOLUTE _var _srcdir _bindir)
  set(_lst "")
  foreach(f ${ARGN})
    if(EXISTS "${f}")
      list(APPEND _lst "${f}")
    elseif(EXISTS "${_srcdir}/${f}")
      list(APPEND _lst "${_srcdir}/${f}")
    else()
      list(APPEND _lst "${_bindir}/${f}")
    endif()
  endforeach()
  set(${_var} "${_lst}" PARENT_SCOPE)
endfunction()

# EFL_PKG_CONFIG_EVAL_TO(Var Name [module1 ... moduleN])
#
# Evaluate the list of of pkg-config modules and assign to variable
# Var. If it's missing, abort with a message saying ${Name} is missing
# the list of modules.
#
# OPTIONAL keyword may be used to convert the remaining elements in optional
# packages.
function(EFL_PKG_CONFIG_EVAL_TO _var _name)
  set(_found "")
  set(_missing "")
  set(_missing_optional "")
  set(_optional OFF)
  foreach(f ${ARGN})
    if(${f} STREQUAL "OPTIONAL")
      set(_optional ON)
    else()
      pkg_check_modules(PKG_CONFIG_DEP_${f} ${f})
      if(PKG_CONFIG_DEP_${f}_FOUND)
        list(APPEND _found ${f})
      elseif(_optional)
        list(APPEND _missing_optional ${f})
        LIST_APPEND_GLOBAL(EFL_PKG_CONFIG_MISSING_OPTIONAL ${f})
      else()
        list(APPEND _missing ${f})
      else()
      endif()
    endif()
  endforeach()
  if(NOT _missing)
    SET_GLOBAL(${_var} "${_found}")
    SET_GLOBAL(${_var}_MISSING "${_missing_optional}")
  else()
    message(FATAL_ERROR "${_name} missing required pkg-config modules: ${_missing}")
  endif()
endfunction()

# EFL_PKG_CONFIG_EVAL(Name Private_List Public_List)
#
# Evaluates both lists and creates ${Name}_PKG_CONFIG_REQUIRES as well as
# ${Name}_PKG_CONFIG_REQUIRES_PRIVATE with found elements.
#
# OPTIONAL keyword may be used to convert the remaining elements in optional
# packages.
function(EFL_PKG_CONFIG_EVAL _target _private _public)
  EFL_PKG_CONFIG_EVAL_TO(${_target}_PKG_CONFIG_REQUIRES ${_target} ${_public})
  EFL_PKG_CONFIG_EVAL_TO(${_target}_PKG_CONFIG_REQUIRES_PRIVATE ${_target} ${_private})

  set(_lst ${${_target}_PKG_CONFIG_REQUIRES_MISSING})
  foreach(_e ${${_target}_PKG_CONFIG_REQUIRES_PRIVATE_MISSING})
    list(APPEND _lst ${_e})
  endforeach()
  if(_lst)
    message(STATUS "${_target} missing optional pkg-config: ${_lst}")
  endif()
endfunction()

function(EFL_PKG_CONFIG_LIB_WRITE)
  set(_pkg_config_requires)
  set(_pkg_config_requires_private)
  set(_libraries)
  set(_public_libraries)

  foreach(_e ${${EFL_LIB_CURRENT}_PKG_CONFIG_REQUIRES})
    set(_pkg_config_requires "${_pkg_config_requires} ${_e}")
  endforeach()

  foreach(_e ${${EFL_LIB_CURRENT}_PKG_CONFIG_REQUIRES_PRIVATE})
    set(_pkg_config_requires_private "${_pkg_config_requires_private} ${_e}")
  endforeach()

  foreach(_e ${LIBRARIES})
    if(TARGET ${_e})
      set(_pkg_config_requires_private "${_pkg_config_requires_private} ${_e}>=${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
    else()
      set(_libraries "${_libraries} -l${_e}")
    endif()
  endforeach()

  foreach(_e ${PUBLIC_LIBRARIES})
    if(TARGET ${_e})
      set(_pkg_config_requires "${_pkg_config_requires} ${_e}>=${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
    else()
      set(_public_libraries "${_public_libraries} -l${_e}")
    endif()
  endforeach()

  if(NOT ${EFL_LIB_CURRENT} STREQUAL "efl")
    set(_cflags " -I\${includedir}/${EFL_LIB_CURRENT}-${PROJECT_VERSION_MAJOR}")
  endif()

  # TODO: handle eolian needs

  set(_contents
"prefix=${CMAKE_INSTALL_PREFIX}
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include
datarootdir=\${prefix}/share
datadir=\${datarootdir}
pkgdatadir=\${datadir}/${EFL_LIB_CURRENT}
modules=\${libdir}/${EFL_LIB_CURRENT}/modules

Name: ${EFL_LIB_CURRENT}
Description: ${DESCRIPTION}
Version: ${VERSION}
Requires:${_pkg_config_requires}
Requires.private:${_pkg_config_requires_private}
Libs: -L\${libdir} -l${EFL_LIB_CURRENT}${_public_libraries}
Libs.private:${_libraries}
Cflags: -I\${includedir}/efl-${PROJECT_VERSION_MAJOR}${_cflags}
")
  file(WRITE "${CMAKE_BINARY_DIR}/lib/pkgconfig/${EFL_LIB_CURRENT}.pc" "${_contents}")
  install(FILES "${CMAKE_BINARY_DIR}/lib/pkgconfig/${EFL_LIB_CURRENT}.pc"
    DESTINATION "lib/pkgconfig")
endfunction()

# _EFL_INCLUDE_OR_DETECT(Name Source_Dir)
#
# Internal macro that will include(${Source_Dir}/CMakeLists.txt) if
# that exists, otherwise will check if there is a single source file,
# in that case it will automatically define SOURCES to that (including
# extras such as headers and .eo)
#
# Name is only used to print out messages when it's auto-detected.
macro(_EFL_INCLUDE_OR_DETECT _name _srcdir)
  if(EXISTS ${_srcdir}/CMakeLists.txt)
    include(${_srcdir}/CMakeLists.txt)
  else()
    # doc says it's not recommended because it can't know if more files
    # were added, but we're doing this explicitly to handle one file.
    file(GLOB _autodetect_files RELATIVE ${_srcdir}
        ${_srcdir}/*.c
        ${_srcdir}/*.h
        ${_srcdir}/*.hh
        ${_srcdir}/*.cxx
        ${_srcdir}/*.cpp
        ${_srcdir}/*.eo
        )
    list(LENGTH _autodetect_files _autodetect_files_count)
    if(_autodetect_files_count GREATER 1)
      message(WARNING "${_name}: ${_srcdir} contains no CMakeLists.txt and contains more than one source file. Don't know what to do, then ignored.")
    elseif(_autodetect_files_count EQUAL 1)
      file(GLOB SOURCES RELATIVE ${_srcdir}
        ${_srcdir}/*.c
        ${_srcdir}/*.h
        ${_srcdir}/*.hh
        ${_srcdir}/*.cxx
        ${_srcdir}/*.cpp
        ${_srcdir}/*.eo
        )
      message(STATUS "${_name} auto-detected as: ${SOURCES}")
    else()
      message(STATUS "${_name} contains no auto-detectable sources.")
    endif()
    unset(_autodetect_files_count)
    unset(_autodetect_files)
  endif()
endmacro()

# _EFL_LIB_PROCESS_MODULES_INTERNAL()
#
# Internal function to process modules of current EFL_LIB()
function(_EFL_LIB_PROCESS_MODULES_INTERNAL)
  unset(${EFL_LIB_CURRENT}_MODULES CACHE)
  unset(${EFL_LIB_CURRENT}_STATIC_MODULES CACHE)

  if(EXISTS ${EFL_MODULES_SOURCE_DIR}/CMakeLists.txt)
    message(FATAL_ERROR "${EFL_MODULES_SOURCE_DIR}/CMakeLists.txt shouldn't exist. Modules are expected to be defined in their own directory.")
  else()
    file(GLOB modules RELATIVE ${EFL_MODULES_SOURCE_DIR} ${EFL_MODULES_SOURCE_DIR}/*)
    foreach(module ${modules})
      if(IS_DIRECTORY ${EFL_MODULES_SOURCE_DIR}/${module})
        set(EFL_MODULE_SCOPE ${module})

        file(GLOB submodules RELATIVE ${EFL_MODULES_SOURCE_DIR}/${EFL_MODULE_SCOPE} ${EFL_MODULES_SOURCE_DIR}/${EFL_MODULE_SCOPE}/*)
        foreach(submodule ${submodules})
          if(IS_DIRECTORY ${EFL_MODULES_SOURCE_DIR}/${EFL_MODULE_SCOPE}/${submodule})
            EFL_MODULE(${submodule})
          endif()
          unset(submodule)
          unset(submodules)
        endforeach()
      else()
        set(EFL_MODULE_SCOPE)
        EFL_MODULE(${module})
      endif()
      unset(EFL_MODULE_SCOPE)
    endforeach()
  endif()

  if(${EFL_LIB_CURRENT}_MODULES)
    add_custom_target(${EFL_LIB_CURRENT}-modules DEPENDS ${${EFL_LIB_CURRENT}_MODULES})
  endif()
endfunction()

# _EFL_LIB_PROCESS_BINS_INTERNAL()
#
# Internal function to process bins of current EFL_LIB()
function(_EFL_LIB_PROCESS_BINS_INTERNAL)
  if(EXISTS ${EFL_BIN_SOURCE_DIR}/CMakeLists.txt)
    EFL_BIN(${EFL_LIB_CURRENT})
  else()
    file(GLOB bins RELATIVE ${EFL_BIN_SOURCE_DIR} ${EFL_BIN_SOURCE_DIR}/*)
    foreach(bin ${bins})
      if(IS_DIRECTORY ${EFL_BIN_SOURCE_DIR}/${bin})
        EFL_BIN(${bin})
      endif()
    endforeach()
  endif()

  if(${EFL_LIB_CURRENT}_BINS)
    add_custom_target(${EFL_LIB_CURRENT}-bins DEPENDS ${${EFL_LIB_CURRENT}_BINS})
  endif()
endfunction()

# _EFL_LIB_PROCESS_TESTS_INTERNAL()
#
# Internal function to process tests of current EFL_LIB()
function(_EFL_LIB_PROCESS_TESTS_INTERNAL)
  unset(${EFL_LIB_CURRENT}_TESTS CACHE)

  if(EXISTS ${EFL_TESTS_SOURCE_DIR}/CMakeLists.txt)
    EFL_TEST(${EFL_LIB_CURRENT})
  else()
    file(GLOB tests RELATIVE ${EFL_TESTS_SOURCE_DIR} ${EFL_TESTS_SOURCE_DIR}/*)
    foreach(test ${tests})
      if(IS_DIRECTORY ${EFL_TESTS_SOURCE_DIR}/${test})
        EFL_TEST(${test})
      endif()
    endforeach()
  endif()

  if(${EFL_LIB_CURRENT}_TESTS)
    add_custom_target(${EFL_LIB_CURRENT}-tests DEPENDS ${${EFL_LIB_CURRENT}_TESTS})
    LIST_APPEND_GLOBAL(EFL_ALL_TESTS ${EFL_LIB_CURRENT}-tests)
  endif()
endfunction()

# EFL_LIB(Name)
#
# adds a library ${Name} automatically setting object/target
# properties based on script-modifiable variables:
#  - DESCRIPTION: results in ${Name}_DESCRIPTION and fills pkg-config files.
#  - PKG_CONFIG_REQUIRES: results in ${Name}_PKG_CONFIG_REQUIRES and
#    fills pkg-config files. Elements after 'OPTIONAL' keyword are
#    optional.
#  - PKG_CONFIG_REQUIRES_PRIVATE: results in
#    ${Name}_PKG_CONFIG_REQUIRES_PRIVATE and fills pkg-config
#    files. Elements after 'OPTIONAL' keyword are optional.
#  - INCLUDE_DIRECTORIES: results in target_include_directories
#  - SYSTEM_INCLUDE_DIRECTORIES: results in target_include_directories(SYSTEM)
#  - OUTPUT_NAME
#  - SOURCES source files that are needed, eo files can also be added here to be build
#  - PUBLIC_HEADERS
#  - VERSION (defaults to project version)
#  - SOVERSION (defaults to project major version)
#  - LIBRARY_TYPE: SHARED or STATIC, defaults to SHARED
#  - OBJECT_DEPENDS: say this object depends on other files (ie: includes)
#  - DEPENDENCIES: results in add_dependencies()
#  - LIBRARIES: results in target_link_libraries(LINK_PRIVATE)
#  - PUBLIC_LIBRARIES: results in target_link_libraries(LINK_PUBLIC)
#  - DEFINITIONS: target_compile_definitions()
#  - PUBLIC_EO_FILES: the eo files will be used to build that lib, and will be installed to the filesystem
#
# Defines the following variables that can be used within the included files:
#  - EFL_LIB_CURRENT to ${Name}
#  - EFL_LIB_SOURCE_DIR to source dir of ${Name} libraries
#  - EFL_LIB_BINARY_DIR to binary dir of ${Name} libraries
#  - EFL_BIN_SOURCE_DIR to source dir of ${Name} executables
#  - EFL_BIN_BINARY_DIR to binary dir of ${Name} executables
#  - EFL_MODULES_SOURCE_DIR to source dir of ${Name} modules
#  - EFL_MODULES_BINARY_DIR to binary dir of ${Name} modules
#  - EFL_TESTS_SOURCE_DIR to source dir of ${Name} tests
#  - EFL_TESTS_BINARY_DIR to binary dir of ${Name} tests
#
# Modules are processed like:
#   - loop for directories in src/modules/${EFL_LIB_CURRENT}:
#      - if a src/modules/${EFL_LIB_CURRENT}/${Module}/CMakeLists.txt
#        use variables as documented in EFL_MODULE()
#      - otherwise loop for scoped-modules in
#        src/modules/${EFL_LIB_CURRENT}/${EFL_MODULE_SCOPE}/CMakeLists.txt
#        and use variables as documented in EFL_MODULE()
#
# EFL_MODULE() will handle MODULE_TYPE=ON;OFF;STATIC, handling
# dependencies and installation in the proper path, considering
# ${EFL_MODULE_SCOPE} whenever it's set.
#
# Binaries and tests are processed similarly:
#   - if src/bin/${EFL_LIB_CURRENT}/CMakeLists.txt exist, then use
#     variables as documented in EFL_BIN() or EFL_TEST().  The target
#     will be called ${EFL_LIB_CURRENT}-bin or ${EFL_LIB_CURRENT}-test
#     and the test OUTPUT_NAME defaults to ${EFL_LIB_CURRENT}_suite.
#   - otherwise loop for directories in src/bin/${EFL_LIB_CURRENT} and
#     for each src/bin/${EFL_LIB_CURRENT}/${Entry}/CMakeLists.txt use
#     variables as documented in EFL_BIN() or EFL_TEST().  Binaries
#     must provide an unique name that will be used as both target and
#     OUTPUT_NAME. Tests will generate targets
#     ${EFL_LIB_CURRENT}-test-${Entry}, while OUTPUT_NAME is ${Entry}.
#
# NOTE: src/modules/${EFL_LIB_CURRENT}/CMakeLists.txt is not
#       allowed as it makes no sense to have a single module named
#       after the library.
#
function(EFL_LIB _target)
  set(EFL_LIB_CURRENT ${_target})
  set(EFL_LIB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/lib/${_target})
  set(EFL_LIB_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/lib/${_target})
  set(EFL_BIN_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/bin/${_target})
  set(EFL_BIN_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/bin/${_target})
  set(EFL_MODULES_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/modules/${_target})
  set(EFL_MODULES_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/modules/${_target})
  set(EFL_TESTS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/tests/${_target})
  set(EFL_TESTS_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/tests/${_target})

  set(DESCRIPTION)
  set(PKG_CONFIG_REQUIRES)
  set(PKG_CONFIG_REQUIRES_PRIVATE)
  set(INCLUDE_DIRECTORIES)
  set(SYSTEM_INCLUDE_DIRECTORIES)
  set(OUTPUT_NAME)
  set(SOURCES)
  set(PUBLIC_HEADERS)
  set(VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
  set(SOVERSION ${PROJECT_VERSION_MAJOR})
  set(LIBRARY_TYPE SHARED)
  set(OBJECT_DEPENDS)
  set(DEPENDENCIES)
  set(LIBRARIES)
  set(PUBLIC_LIBRARIES)
  set(DEFINITIONS)
  set(PUBLIC_EO_FILES)

  include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config/${_target}.cmake OPTIONAL)
  include(${EFL_LIB_SOURCE_DIR}/CMakeLists.txt OPTIONAL)
  if(LIBRARY_TYPE STREQUAL SHARED AND NOT PUBLIC_HEADERS)
    message(FATAL_ERROR "Shared libraries must install public headers!")
  endif()

  #merge public eo files into sources
  set(SOURCES ${SOURCES} ${PUBLIC_EO_FILES})

  EFL_FILES_TO_ABSOLUTE(_headers ${EFL_LIB_SOURCE_DIR} ${EFL_LIB_BINARY_DIR}
    ${PUBLIC_HEADERS})
  EFL_FILES_TO_ABSOLUTE(_sources ${EFL_LIB_SOURCE_DIR} ${EFL_LIB_BINARY_DIR}
    ${SOURCES})
  EFL_FILES_TO_ABSOLUTE(_obj_deps ${EFL_LIB_SOURCE_DIR} ${EFL_LIB_BINARY_DIR}
    ${OBJECT_DEPENDS})
  EFL_FILES_TO_ABSOLUTE(_public_eo_files ${EFL_LIB_SOURCE_DIR} ${EFL_LIB_BINARY_DIR}
    ${PUBLIC_EO_FILES})

  foreach(public_eo_file ${PUBLIC_EO_FILES})
    list(APPEND _headers ${EFL_LIB_BINARY_DIR}/${public_eo_file}.h)
  endforeach()
  unset(public_eo_file)

  EFL_PKG_CONFIG_EVAL(${_target} "${PKG_CONFIG_REQUIRES_PRIVATE}" "${PKG_CONFIG_REQUIRES}")


  add_library(${_target} ${LIBRARY_TYPE} ${_sources} ${_headers})
  set_target_properties(${_target} PROPERTIES
    FRAMEWORK TRUE
    PUBLIC_HEADER "${_headers}"
    OBJECT_DEPENDS "${_obj_deps}"
    COMPILE_FLAGS -DPACKAGE_DATA_DIR=\\"${CMAKE_INSTALL_FULL_DATADIR}/${_target}/\\")

  if(DEPENDENCIES)
    add_dependencies(${_target} ${DEPENDENCIES})
  endif()

  if(LIBRARIES)
    target_link_libraries(${_target} LINK_PRIVATE ${LIBRARIES})
  endif()
  if(PUBLIC_LIBRARIES)
    target_link_libraries(${_target} PUBLIC ${PUBLIC_LIBRARIES})
  endif()

  target_include_directories(${_target} PUBLIC
    ${INCLUDE_DIRECTORIES}
    ${EFL_LIB_SOURCE_DIR}
    ${EFL_LIB_BINARY_DIR}
    )
  if(SYSTEM_INCLUDE_DIRECTORIES)
    target_include_directories(${_target} SYSTEM PUBLIC ${SYSTEM_INCLUDE_DIRECTORIES})
  endif()

  if(DEFINITIONS)
    target_compile_definitions(${_target} PRIVATE ${DEFINITIONS})
  endif()

  if(OUTPUT_NAME)
    set_target_properties(${_target} PROPERTIES OUTPUT_NAME ${OUTPUT_NAME})
  endif()

  if(VERSION AND SOVERSION)
    set_target_properties(${_target} PROPERTIES
      VERSION ${VERSION}
      SOVERSION ${SOVERSION})
  endif()

  EFL_CREATE_EO_RULES(${_target} ${EFL_LIB_BINARY_DIR})

  EFL_PKG_CONFIG_LIB_WRITE()

  install(TARGETS ${_target}
    PUBLIC_HEADER DESTINATION include/${_target}-${PROJECT_VERSION_MAJOR}
    RUNTIME DESTINATION bin
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib)
  install(FILES
    ${_public_eo_files} DESTINATION share/eolian/include/${_target}-${PROJECT_VERSION_MAJOR}
    )
  # do not leak those into binaries, modules or tests
  unset(_sources)
  unset(_headers)
  unset(_obj_deps)
  unset(_public_eo_files)
  unset(INCLUDE_DIRECTORIES)
  unset(SYSTEM_INCLUDE_DIRECTORIES)
  unset(OUTPUT_NAME)
  unset(SOURCES)
  unset(PUBLIC_HEADERS)
  unset(VERSION)
  unset(SOVERSION)
  unset(LIBRARY_TYPE)
  unset(OBJECT_DEPENDS)
  unset(DEPENDENCIES)
  unset(LIBRARIES)
  unset(PUBLIC_LIBRARIES)
  unset(DEFINITIONS)
  unset(DESCRIPTION)
  unset(PUBLIC_EO_FILES)
  unset(PKG_CONFIG_REQUIRES)
  unset(PKG_CONFIG_REQUIRES_PRIVATE)

  _EFL_LIB_PROCESS_BINS_INTERNAL()
  _EFL_LIB_PROCESS_MODULES_INTERNAL()
  _EFL_LIB_PROCESS_TESTS_INTERNAL()

  include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/post/${_target}.cmake OPTIONAL)

  LIST_APPEND_GLOBAL(EFL_ALL_LIBS ${_target})
endfunction()

# EFL_BIN(Name)
#
# Adds a binary (executable) for ${EFL_LIB_CURRENT} using
# ${EFL_BIN_SOURCE_DIR} and ${EFL_BIN_BINARY_DIR}
#
# Settings:
#  - INCLUDE_DIRECTORIES: results in target_include_directories
#  - SYSTEM_INCLUDE_DIRECTORIES: results in target_include_directories(SYSTEM)
#  - OUTPUT_NAME
#  - SOURCES
#  - OBJECT_DEPENDS: say this object depends on other files (ie: includes)
#  - DEPENDENCIES: results in add_dependencies(), defaults to
#    ${EFL_LIB_CURRENT}-modules
#  - LIBRARIES: results in target_link_libraries()
#  - DEFINITIONS: target_compile_definitions()
#  - INSTALL_DIR: defaults to bin. If empty, won't install.
#
# NOTE: it's meant to be called by files included by EFL_LIB() or similar,
# otherwise you need to prepare the environment yourself.
function(EFL_BIN _binname)
  set(INCLUDE_DIRECTORIES)
  set(SYSTEM_INCLUDE_DIRECTORIES)
  set(OUTPUT_NAME ${_binname})
  set(SOURCES)
  set(OBJECT_DEPENDS)
  if(TARGET ${EFL_LIB_CURRENT}-modules)
    set(DEPENDENCIES ${EFL_LIB_CURRENT}-modules)
  else()
    set(DEPENDENCIES)
  endif()
  set(LIBRARIES)
  set(DEFINITIONS)
  set(INSTALL ON)
  set(INSTALL_DIR bin)

  if(_binname STREQUAL ${EFL_LIB_CURRENT})
    set(_binsrcdir "${EFL_BIN_SOURCE_DIR}")
    set(_binbindir "${EFL_BIN_BINARY_DIR}")
    set(_bintarget "${EFL_LIB_CURRENT}-bin") # otherwise target would exist
  else()
    set(_binsrcdir "${EFL_BIN_SOURCE_DIR}/${_binname}")
    set(_binbindir "${EFL_BIN_BINARY_DIR}/${_binname}")
    set(_bintarget "${_binname}")
  endif()

  _EFL_INCLUDE_OR_DETECT("Binary ${_bintarget}" ${_binsrcdir})

  if(NOT SOURCES)
    message(WARNING "${_binsrcdir}/CMakeLists.txt defines no SOURCES")
    return()
  endif()
  if(PUBLIC_HEADERS)
    message(WARNING "${_binsrcdir}/CMakeLists.txt should not define PUBLIC_HEADERS, it's not to be installed.")
  endif()

  EFL_FILES_TO_ABSOLUTE(_sources ${_binsrcdir} ${_binbindir} ${SOURCES})
  EFL_FILES_TO_ABSOLUTE(_obj_deps ${_binsrcdir} ${_binbindir} ${OBJECT_DEPENDS})

  add_executable(${_bintarget} ${_sources})

  if(_obj_deps)
    set_target_properties(${_bintarget} PROPERTIES
      OBJECT_DEPENDS "${_obj_deps}")
  endif()

  if(DEPENDENCIES)
    add_dependencies(${_bintarget} ${DEPENDENCIES})
  endif()

  target_include_directories(${_bintarget} PRIVATE
    ${_binrcdir}
    ${_binbindir}
    ${INCLUDE_DIRECTORIES})
  if(SYSTEM_INCLUDE_DIRECTORIES)
    target_include_directories(${_bintarget} SYSTEM PRIVATE
      ${SYSTEM_INCLUDE_DIRECTORIES})
  endif()
  target_link_libraries(${_bintarget} LINK_PRIVATE
    ${EFL_LIB_CURRENT}
    ${LIBRARIES})

  if(DEFINITIONS)
    target_compile_definitions(${_bintarget} PRIVATE ${DEFINITIONS})
  endif()

  if(OUTPUT_NAME)
    set_target_properties(${_bintarget} PROPERTIES OUTPUT_NAME ${OUTPUT_NAME})
  endif()

  if(INSTALL_DIR)
    install(TARGETS ${_bintarget} RUNTIME DESTINATION ${INSTALL_DIR})
  endif()
endfunction()

# EFL_TEST(Name)
#
# Adds a test for ${EFL_LIB_CURRENT} using
# ${EFL_TESTS_SOURCE_DIR} and ${EFL_TESTS_BINARY_DIR}
#
# Settings:
#  - INCLUDE_DIRECTORIES: results in target_include_directories
#  - SYSTEM_INCLUDE_DIRECTORIES: results in target_include_directories(SYSTEM)
#  - OUTPUT_NAME
#  - SOURCES
#  - OBJECT_DEPENDS: say this object depends on other files (ie: includes)
#  - DEPENDENCIES: results in add_dependencies(), defaults to
#    ${EFL_LIB_CURRENT}-modules
#  - LIBRARIES: results in target_link_libraries()
#  - DEFINITIONS: target_compile_definitions()
#
# NOTE: it's meant to be called by files included by EFL_LIB() or similar,
# otherwise you need to prepare the environment yourself.
function(EFL_TEST _testname)
  if(NOT CHECK_FOUND)
    message(STATUS "${EFL_LIB_CURRENT} test ${_testname} ignored since no 'check' library was found.")
    return()
  endif()

  set(INCLUDE_DIRECTORIES)
  set(SYSTEM_INCLUDE_DIRECTORIES)
  set(OUTPUT_NAME ${_testname})
  set(SOURCES)
  set(OBJECT_DEPENDS)
  if(TARGET ${EFL_LIB_CURRENT}-modules)
    set(DEPENDENCIES ${EFL_LIB_CURRENT}-modules)
  else()
    set(DEPENDENCIES)
  endif()
  set(LIBRARIES)
  set(DEFINITIONS)

  if(_testname STREQUAL ${EFL_LIB_CURRENT})
    set(_testsrcdir "${EFL_TESTS_SOURCE_DIR}")
    set(_testbindir "${EFL_TESTS_BINARY_DIR}")
    set(_testtarget "${EFL_LIB_CURRENT}-test") # otherwise target would exist
    set(OUTPUT_NAME "${EFL_LIB_CURRENT}_suite") # backward compatible
  else()
    set(_testsrcdir "${EFL_TESTS_SOURCE_DIR}/${_testname}")
    set(_testbindir "${EFL_TESTS_BINARY_DIR}/${_testname}")
    set(_testtarget "${EFL_LIB_CURRENT}-test-${_testname}")
  endif()

  _EFL_INCLUDE_OR_DETECT("Test ${_testtarget}" ${_testsrcdir})

  if(NOT SOURCES)
    message(WARNING "${_testsrcdir}/CMakeLists.txt defines no SOURCES")
    return()
  endif()
  if(PUBLIC_HEADERS)
    message(WARNING "${_testsrcdir}/CMakeLists.txt should not define PUBLIC_HEADERS, it's not to be installed.")
  endif()

  EFL_FILES_TO_ABSOLUTE(_sources ${_testsrcdir} ${_testbindir} ${SOURCES})
  EFL_FILES_TO_ABSOLUTE(_obj_deps ${_testsrcdir} ${_testbindir} ${OBJECT_DEPENDS})

  add_executable(${_testtarget} EXCLUDE_FROM_ALL ${_sources})

  if(_obj_deps)
    set_target_properties(${_testtarget} PROPERTIES
      OBJECT_DEPENDS "${_obj_deps}")
  endif()

  if(DEPENDENCIES)
    add_dependencies(${_testtarget} ${DEPENDENCIES})
  endif()

  target_include_directories(${_testtarget} PRIVATE
    ${_testrcdir}
    ${_testbindir}
    ${INCLUDE_DIRECTORIES})
  target_include_directories(${_testtarget} SYSTEM PRIVATE
    ${SYSTEM_INCLUDE_DIRECTORIES}
    ${CHECK_INCLUDE_DIRS})
  target_link_libraries(${_testtarget} LINK_PRIVATE
    ${EFL_LIB_CURRENT}
    ${LIBRARIES}
    ${CHECK_LIBRARIES})

  target_compile_definitions(${_testtarget} PRIVATE
    "-DPACKAGE_DATA_DIR=\"${EFL_TESTS_SOURCE_DIR}\""
    "-DTESTS_SRC_DIR=\"${_testrcdir}\""
    "-DTESTS_BUILD_DIR=\"${_testbindir}\""
    "-DTESTS_WD=\"${PROJECT_BINARY_DIR}\""
    ${DEFINITIONS}
    )

  if(OUTPUT_NAME)
    set_target_properties(${_testtarget} PROPERTIES OUTPUT_NAME ${OUTPUT_NAME})
  endif()

  set_target_properties(${_testtarget} PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY "${_testbindir}"
    RUNTIME_OUTPUT_DIRECTORY "${_testbindir}")

  add_test(NAME ${_testtarget} COMMAND ${_testtarget})
  LIST_APPEND_GLOBAL(${EFL_LIB_CURRENT}_TESTS ${_testtarget})

  add_test(${_testtarget}-build "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target ${_testtarget})
  set_tests_properties(${_testtarget} PROPERTIES DEPENDS ${_testtarget}-build)
endfunction()

# EFL_MODULE(Name)
#
# Adds a module for ${EFL_LIB_CURRENT} using
# ${EFL_MODULES_SOURCE_DIR} and ${EFL_MODULES_BINARY_DIR}
# as well as ${EFL_MODULE_SCOPE} if it's contained into
# a subdir, such as eina's "mp" or evas "engines".
#
# To keep it simple to use, user is only expected to define variables:
#  - SOURCES
#  - OBJECT_DEPENDS
#  - LIBRARIES
#  - INCLUDE_DIRECTORIES
#  - SYSTEM_INCLUDE_DIRECTORIES
#  - DEFINITIONS
#  - MODULE_TYPE: one of ON;OFF;STATIC, defaults to ON
#  - INSTALL_DIR: defaults to
#    lib/${EFL_LIB_CURRENT}/modules/${EFL_MODULE_SCOPE}/${Name}/v-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}/.
#    If empty, won't install.
#
# NOTE: since the file will be included it shouldn't mess with global variables!
function(EFL_MODULE _modname)
  if(EFL_MODULE_SCOPE)
    set(_modsrcdir ${EFL_MODULES_SOURCE_DIR}/${EFL_MODULE_SCOPE}/${_modname})
    set(_modoutdir lib/${EFL_LIB_CURRENT}/modules/${EFL_MODULE_SCOPE}/${_modname}/v-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})
    set(_modbindir ${EFL_MODULES_BINARY_DIR}/${EFL_MODULE_SCOPE}/${_modname})
    set(_modtarget ${EFL_LIB_CURRENT}-module-${EFL_MODULE_SCOPE}-${_modname})
    string(TOUPPER "${EFL_LIB_CURRENT}_MODULE_TYPE_${EFL_MODULE_SCOPE}_${_modname}" _modoptionname)
    if(NOT ${_modoptionname}_DEFAULT)
      set(${_modoptionname}_DEFAULT "ON")
    endif()
    EFL_OPTION(${_modoptionname} "Build ${EFL_LIB_CURRENT} module ${EFL_MODULE_SCOPE}/${_modname}" ${${_modoptionname}_DEFAULT} CHOICE ON;OFF;STATIC)
  else()
    set(_modsrcdir ${EFL_MODULES_SOURCE_DIR}/${_modname})
    set(_modoutdir lib/${EFL_LIB_CURRENT}/modules/${_modname}/v-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})
    set(_modbindir ${EFL_MODULES_BINARY_DIR}/${_modname})
    set(_modtarget ${EFL_LIB_CURRENT}-module-${_modname})
    string(TOUPPER "${EFL_LIB_CURRENT}_MODULE_TYPE_${_modname}" _modoptionname)
    if(NOT ${_modoptionname}_DEFAULT)
      set(${_modoptionname}_DEFAULT "ON")
    endif()
    EFL_OPTION(${_modoptionname} "Build ${EFL_LIB_CURRENT} module ${_modname}" ${${_modoptionname}_DEFAULT} CHOICE ON;OFF;STATIC)
  endif()

  set(SOURCES)
  set(OBJECT_DEPENDS)
  set(LIBRARIES)
  set(INCLUDE_DIRECTORIES)
  set(SYSTEM_INCLUDE_DIRECTORIES)
  set(DEFINITIONS)
  set(MODULE_TYPE "${${_modoptionname}}")
  set(INSTALL_DIR ${_modoutdir})

  _EFL_INCLUDE_OR_DETECT("Module ${_modtarget}" ${_modsrcdir})

  if(NOT SOURCES)
    message(WARNING "${_modsrcdir}/CMakeLists.txt defines no SOURCES")
    return()
  endif()
  if(PUBLIC_HEADERS)
    message(WARNING "${_modsrcdir}/CMakeLists.txt should not define PUBLIC_HEADERS, it's not to be installed.")
  endif()

  if("${MODULE_TYPE}" STREQUAL "OFF")
    return()
  elseif("${MODULE_TYPE}" STREQUAL "STATIC")
    set(_modtype STATIC)
  else()
    set(_modtype MODULE)
  endif()

  EFL_FILES_TO_ABSOLUTE(_sources ${_modsrcdir} ${_modbindir} ${SOURCES})
  EFL_FILES_TO_ABSOLUTE(_obj_deps ${_modsrcdir} ${_modbindir} ${OBJECT_DEPENDS})

  add_library(${_modtarget} ${_modtype} ${_sources})
  set_target_properties(${_modtarget} PROPERTIES
    OBJECT_DEPENDS "${_obj_deps}"
    PREFIX ""
    OUTPUT_NAME "module")

  target_include_directories(${_modtarget} PRIVATE
    ${_modsrcdir}
    ${_modbindir}
    ${INCLUDE_DIRECTORIES})
  target_include_directories(${_modtarget} SYSTEM PUBLIC
    ${SYSTEM_INCLUDE_DIRECTORIES})
  target_link_libraries(${_modtarget} LINK_PRIVATE ${LIBRARIES})

  target_compile_definitions(${_modtarget} PRIVATE ${DEFINITIONS})

  set_target_properties(${_modtarget} PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY "${_modoutdir}"
    ARCHIVE_OUTPUT_DIRECTORY "${_modoutdir}"
    RUNTIME_OUTPUT_DIRECTORY "${_modoutdir}")

  if("${MODULE_TYPE}" STREQUAL "STATIC")
    target_link_libraries(${EFL_LIB_CURRENT} LINK_PRIVATE ${_modtarget})
    target_compile_definitions(${EFL_LIB_CURRENT} PRIVATE "-D${_modoptionname}_STATIC=1")
    target_include_directories(${_modtarget} PRIVATE
      ${EFL_LIB_SOURCE_DIR}
      ${EFL_LIB_BINARY_DIR})
    set_target_properties(${_modtarget} PROPERTIES
      POSITION_INDEPENDENT_CODE TRUE)

    LIST_APPEND_GLOBAL(${EFL_LIB_CURRENT}_STATIC_MODULES ${_modtarget})
  else()
    target_link_libraries(${_modtarget} LINK_PRIVATE ${EFL_LIB_CURRENT})
    target_compile_definitions(${EFL_LIB_CURRENT} PRIVATE "-D${_modoptionname}_DYNAMIC=1")
    LIST_APPEND_GLOBAL(${EFL_LIB_CURRENT}_MODULES ${_modtarget})
    if(INSTALL_DIR)
      install(TARGETS ${_modtarget} LIBRARY DESTINATION "${INSTALL_DIR}")
    endif()
  endif()
endfunction()

macro(EFL_PROJECT version)
  if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
      project(efl VERSION ${version})
  else ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    execute_process(
      COMMAND git rev-list --count HEAD
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
      OUTPUT_VARIABLE GIT_VERSION
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    project(efl VERSION ${version}.${GIT_VERSION})
  endif ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
  message("VERSION ${PROJECT_VERSION}")
endmacro()

# Will use the source of the given target to create rules for creating
# the .eo.c and .eo.h files. The INCLUDE_DIRECTORIES of the target will be used
function(EFL_CREATE_EO_RULES target generation_dir)
   get_target_property(source_files ${target} SOURCES)
   get_target_property(link_libraries ${target} LINK_LIBRARIES)

   set(all_libraries ${target} ${link_libraries})
   set(include_cmd "")
   foreach(link_target ${all_libraries})
     list(APPEND include_cmd -I${CMAKE_SOURCE_DIR}/src/lib/${link_target})
   endforeach()

   set(all_eo_gen_files "")
   foreach(file ${source_files})
      get_filename_component(ext ${file} EXT)
      get_filename_component(filename ${file} NAME)

      if(${ext} STREQUAL ".eo")
        set(file_eo_gen_files ${generation_dir}/${filename}.c ${generation_dir}/${filename}.h)
      elseif(${ext} STREQUAL ".eot")
        set(file_eo_gen_files ${generation_dir}/${filename}.h)
      else()
        set(file_eo_gen_files "")
      endif()

      #add the custom rule
      if(file_eo_gen_files)
        add_custom_command(
           OUTPUT ${file_eo_gen_files}
           COMMAND ${CMAKE_COMMAND} -E env "EFL_RUN_IN_TREE=1" ${EOLIAN_BIN} ${include_cmd} -o c:${generation_dir}/${filename}.c -o h:${generation_dir}/${filename}.h ${file}
           DEPENDS ${file}
        )
        list(APPEND all_eo_gen_files ${file_eo_gen_files})
      endif()
    endforeach()
    if(all_eo_gen_files)
      file(MAKE_DIRECTORY ${generation_dir})
      add_custom_target(${target}-eo
        DEPENDS ${all_eo_gen_files}
      )
      add_dependencies(${target} ${target}-eo)
      add_dependencies(${target}-eo eolian-bin)
    endif()
endfunction()