makedep.c 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465
  1. /*
  2. * Generate include file dependencies
  3. *
  4. * Copyright 1996, 2013 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #define NO_LIBWINE_PORT
  22. #include "wine/port.h"
  23. #include <assert.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdarg.h>
  29. #include <signal.h>
  30. #include <string.h>
  31. #ifdef HAVE_UNISTD_H
  32. # include <unistd.h>
  33. #endif
  34. #include "wine/list.h"
  35. struct strarray
  36. {
  37. unsigned int count; /* strings in use */
  38. unsigned int size; /* total allocated size */
  39. const char **str;
  40. };
  41. enum incl_type
  42. {
  43. INCL_NORMAL, /* #include "foo.h" */
  44. INCL_SYSTEM, /* #include <foo.h> */
  45. INCL_IMPORT, /* idl import "foo.idl" */
  46. INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
  47. INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
  48. INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
  49. };
  50. struct dependency
  51. {
  52. int line; /* source line where this header is included */
  53. enum incl_type type; /* type of include */
  54. char *name; /* header name */
  55. };
  56. struct file
  57. {
  58. struct list entry;
  59. char *name; /* full file name relative to cwd */
  60. void *args; /* custom arguments for makefile rule */
  61. unsigned int flags; /* flags (see below) */
  62. unsigned int deps_count; /* files in use */
  63. unsigned int deps_size; /* total allocated size */
  64. struct dependency *deps; /* all header dependencies */
  65. };
  66. struct incl_file
  67. {
  68. struct list entry;
  69. struct file *file;
  70. char *name;
  71. char *filename;
  72. char *sourcename; /* source file name for generated headers */
  73. struct incl_file *included_by; /* file that included this one */
  74. int included_line; /* line where this file was included */
  75. enum incl_type type; /* type of include */
  76. int use_msvcrt; /* put msvcrt headers in the search path? */
  77. struct incl_file *owner;
  78. unsigned int files_count; /* files in use */
  79. unsigned int files_size; /* total allocated size */
  80. struct incl_file **files;
  81. struct strarray dependencies; /* file dependencies */
  82. };
  83. #define FLAG_GENERATED 0x000001 /* generated file */
  84. #define FLAG_INSTALL 0x000002 /* file to install */
  85. #define FLAG_PARENTDIR 0x000004 /* file comes from parent dir */
  86. #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
  87. #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
  88. #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
  89. #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
  90. #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
  91. #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
  92. #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
  93. #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
  94. #define FLAG_RC_PO 0x010000 /* rc file contains translations */
  95. #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
  96. #define FLAG_C_UNIX 0x040000 /* file is part of a Unix library */
  97. #define FLAG_SFD_FONTS 0x080000 /* sfd file generated bitmap fonts */
  98. static const struct
  99. {
  100. unsigned int flag;
  101. const char *ext;
  102. } idl_outputs[] =
  103. {
  104. { FLAG_IDL_TYPELIB, ".tlb" },
  105. { FLAG_IDL_REGTYPELIB, "_t.res" },
  106. { FLAG_IDL_CLIENT, "_c.c" },
  107. { FLAG_IDL_IDENT, "_i.c" },
  108. { FLAG_IDL_PROXY, "_p.c" },
  109. { FLAG_IDL_SERVER, "_s.c" },
  110. { FLAG_IDL_REGISTER, "_r.res" },
  111. { FLAG_IDL_HEADER, ".h" }
  112. };
  113. #define HASH_SIZE 997
  114. static struct list files[HASH_SIZE];
  115. static const struct strarray empty_strarray;
  116. enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
  117. /* variables common to all makefiles */
  118. static struct strarray linguas;
  119. static struct strarray dll_flags;
  120. static struct strarray target_flags;
  121. static struct strarray msvcrt_flags;
  122. static struct strarray extra_cflags;
  123. static struct strarray extra_cross_cflags;
  124. static struct strarray cpp_flags;
  125. static struct strarray lddll_flags;
  126. static struct strarray libs;
  127. static struct strarray enable_tests;
  128. static struct strarray cmdline_vars;
  129. static struct strarray disabled_dirs;
  130. static struct strarray cross_import_libs;
  131. static struct strarray delay_import_libs;
  132. static struct strarray top_install_lib;
  133. static struct strarray top_install_dev;
  134. static const char *root_src_dir;
  135. static const char *tools_dir;
  136. static const char *tools_ext;
  137. static const char *exe_ext;
  138. static const char *dll_ext;
  139. static const char *man_ext;
  140. static const char *crosstarget;
  141. static const char *fontforge;
  142. static const char *convert;
  143. static const char *flex;
  144. static const char *bison;
  145. static const char *ar;
  146. static const char *ranlib;
  147. static const char *rsvg;
  148. static const char *icotool;
  149. static const char *dlltool;
  150. static const char *msgfmt;
  151. static const char *ln_s;
  152. static const char *sed_cmd;
  153. static const char *delay_load_flag;
  154. struct makefile
  155. {
  156. /* values determined from input makefile */
  157. struct strarray vars;
  158. struct strarray include_paths;
  159. struct strarray include_args;
  160. struct strarray define_args;
  161. struct strarray extraincl_args;
  162. struct strarray programs;
  163. struct strarray scripts;
  164. struct strarray imports;
  165. struct strarray subdirs;
  166. struct strarray delayimports;
  167. struct strarray extradllflags;
  168. struct strarray install_lib;
  169. struct strarray install_dev;
  170. struct strarray extra_targets;
  171. struct list sources;
  172. struct list includes;
  173. const char *base_dir;
  174. const char *src_dir;
  175. const char *obj_dir;
  176. const char *top_src_dir;
  177. const char *top_obj_dir;
  178. const char *parent_dir;
  179. const char *module;
  180. const char *testdll;
  181. const char *sharedlib;
  182. const char *staticlib;
  183. const char *staticimplib;
  184. const char *importlib;
  185. int disabled;
  186. int use_msvcrt;
  187. int is_cross;
  188. int is_win16;
  189. int is_exe;
  190. struct makefile **submakes;
  191. /* values generated at output time */
  192. struct strarray in_files;
  193. struct strarray ok_files;
  194. struct strarray clean_files;
  195. struct strarray distclean_files;
  196. struct strarray uninstall_files;
  197. struct strarray object_files;
  198. struct strarray crossobj_files;
  199. struct strarray unixobj_files;
  200. struct strarray res_files;
  201. struct strarray c2man_files;
  202. struct strarray dlldata_files;
  203. struct strarray implib_objs;
  204. struct strarray all_targets;
  205. struct strarray phony_targets;
  206. struct strarray dependencies;
  207. struct strarray install_rules[NB_INSTALL_RULES];
  208. };
  209. static struct makefile *top_makefile;
  210. static const char separator[] = "### Dependencies";
  211. static const char *output_makefile_name = "Makefile";
  212. static const char *input_file_name;
  213. static const char *output_file_name;
  214. static const char *temp_file_name;
  215. static int relative_dir_mode;
  216. static int input_line;
  217. static int output_column;
  218. static FILE *output_file;
  219. static const char Usage[] =
  220. "Usage: makedep [options] [directories]\n"
  221. "Options:\n"
  222. " -R from to Compute the relative path between two directories\n"
  223. " -fxxx Store output in file 'xxx' (default: Makefile)\n";
  224. #ifndef __GNUC__
  225. #define __attribute__(x)
  226. #endif
  227. static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  228. static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  229. static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  230. static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  231. /*******************************************************************
  232. * fatal_error
  233. */
  234. static void fatal_error( const char *msg, ... )
  235. {
  236. va_list valist;
  237. va_start( valist, msg );
  238. if (input_file_name)
  239. {
  240. fprintf( stderr, "%s:", input_file_name );
  241. if (input_line) fprintf( stderr, "%d:", input_line );
  242. fprintf( stderr, " error: " );
  243. }
  244. else fprintf( stderr, "makedep: error: " );
  245. vfprintf( stderr, msg, valist );
  246. va_end( valist );
  247. exit(1);
  248. }
  249. /*******************************************************************
  250. * fatal_perror
  251. */
  252. static void fatal_perror( const char *msg, ... )
  253. {
  254. va_list valist;
  255. va_start( valist, msg );
  256. if (input_file_name)
  257. {
  258. fprintf( stderr, "%s:", input_file_name );
  259. if (input_line) fprintf( stderr, "%d:", input_line );
  260. fprintf( stderr, " error: " );
  261. }
  262. else fprintf( stderr, "makedep: error: " );
  263. vfprintf( stderr, msg, valist );
  264. perror( " " );
  265. va_end( valist );
  266. exit(1);
  267. }
  268. /*******************************************************************
  269. * cleanup_files
  270. */
  271. static void cleanup_files(void)
  272. {
  273. if (temp_file_name) unlink( temp_file_name );
  274. if (output_file_name) unlink( output_file_name );
  275. }
  276. /*******************************************************************
  277. * exit_on_signal
  278. */
  279. static void exit_on_signal( int sig )
  280. {
  281. exit( 1 ); /* this will call the atexit functions */
  282. }
  283. /*******************************************************************
  284. * xmalloc
  285. */
  286. static void *xmalloc( size_t size )
  287. {
  288. void *res;
  289. if (!(res = malloc (size ? size : 1)))
  290. fatal_error( "Virtual memory exhausted.\n" );
  291. return res;
  292. }
  293. /*******************************************************************
  294. * xrealloc
  295. */
  296. static void *xrealloc (void *ptr, size_t size)
  297. {
  298. void *res;
  299. assert( size );
  300. if (!(res = realloc( ptr, size )))
  301. fatal_error( "Virtual memory exhausted.\n" );
  302. return res;
  303. }
  304. /*******************************************************************
  305. * xstrdup
  306. */
  307. static char *xstrdup( const char *str )
  308. {
  309. char *res = strdup( str );
  310. if (!res) fatal_error( "Virtual memory exhausted.\n" );
  311. return res;
  312. }
  313. /*******************************************************************
  314. * strmake
  315. */
  316. static char *strmake( const char* fmt, ... )
  317. {
  318. int n;
  319. size_t size = 100;
  320. va_list ap;
  321. for (;;)
  322. {
  323. char *p = xmalloc (size);
  324. va_start(ap, fmt);
  325. n = vsnprintf (p, size, fmt, ap);
  326. va_end(ap);
  327. if (n == -1) size *= 2;
  328. else if ((size_t)n >= size) size = n + 1;
  329. else return xrealloc( p, n + 1 );
  330. free(p);
  331. }
  332. }
  333. /*******************************************************************
  334. * strendswith
  335. */
  336. static int strendswith( const char* str, const char* end )
  337. {
  338. size_t l = strlen( str );
  339. size_t m = strlen( end );
  340. return l >= m && strcmp(str + l - m, end) == 0;
  341. }
  342. /*******************************************************************
  343. * output
  344. */
  345. static void output( const char *format, ... )
  346. {
  347. int ret;
  348. va_list valist;
  349. va_start( valist, format );
  350. ret = vfprintf( output_file, format, valist );
  351. va_end( valist );
  352. if (ret < 0) fatal_perror( "output" );
  353. if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
  354. else output_column += ret;
  355. }
  356. /*******************************************************************
  357. * strarray_add
  358. */
  359. static void strarray_add( struct strarray *array, const char *str )
  360. {
  361. if (array->count == array->size)
  362. {
  363. if (array->size) array->size *= 2;
  364. else array->size = 16;
  365. array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
  366. }
  367. array->str[array->count++] = str;
  368. }
  369. /*******************************************************************
  370. * strarray_addall
  371. */
  372. static void strarray_addall( struct strarray *array, struct strarray added )
  373. {
  374. unsigned int i;
  375. for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
  376. }
  377. /*******************************************************************
  378. * strarray_exists
  379. */
  380. static int strarray_exists( const struct strarray *array, const char *str )
  381. {
  382. unsigned int i;
  383. for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
  384. return 0;
  385. }
  386. /*******************************************************************
  387. * strarray_add_uniq
  388. */
  389. static void strarray_add_uniq( struct strarray *array, const char *str )
  390. {
  391. if (!strarray_exists( array, str )) strarray_add( array, str );
  392. }
  393. /*******************************************************************
  394. * strarray_addall_uniq
  395. */
  396. static void strarray_addall_uniq( struct strarray *array, struct strarray added )
  397. {
  398. unsigned int i;
  399. for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
  400. }
  401. /*******************************************************************
  402. * strarray_get_value
  403. *
  404. * Find a value in a name/value pair string array.
  405. */
  406. static const char *strarray_get_value( const struct strarray *array, const char *name )
  407. {
  408. int pos, res, min = 0, max = array->count / 2 - 1;
  409. while (min <= max)
  410. {
  411. pos = (min + max) / 2;
  412. if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
  413. if (res < 0) min = pos + 1;
  414. else max = pos - 1;
  415. }
  416. return NULL;
  417. }
  418. /*******************************************************************
  419. * strarray_set_value
  420. *
  421. * Define a value in a name/value pair string array.
  422. */
  423. static void strarray_set_value( struct strarray *array, const char *name, const char *value )
  424. {
  425. int i, pos, res, min = 0, max = array->count / 2 - 1;
  426. while (min <= max)
  427. {
  428. pos = (min + max) / 2;
  429. if (!(res = strcmp( array->str[pos * 2], name )))
  430. {
  431. /* redefining a variable replaces the previous value */
  432. array->str[pos * 2 + 1] = value;
  433. return;
  434. }
  435. if (res < 0) min = pos + 1;
  436. else max = pos - 1;
  437. }
  438. strarray_add( array, NULL );
  439. strarray_add( array, NULL );
  440. for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
  441. array->str[min * 2] = name;
  442. array->str[min * 2 + 1] = value;
  443. }
  444. /*******************************************************************
  445. * strarray_set_qsort
  446. */
  447. static void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
  448. {
  449. if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
  450. }
  451. /*******************************************************************
  452. * output_filename
  453. */
  454. static void output_filename( const char *name )
  455. {
  456. if (output_column + strlen(name) + 1 > 100)
  457. {
  458. output( " \\\n" );
  459. output( " " );
  460. }
  461. else if (output_column) output( " " );
  462. output( "%s", name );
  463. }
  464. /*******************************************************************
  465. * output_filenames
  466. */
  467. static void output_filenames( struct strarray array )
  468. {
  469. unsigned int i;
  470. for (i = 0; i < array.count; i++) output_filename( array.str[i] );
  471. }
  472. /*******************************************************************
  473. * output_rm_filenames
  474. */
  475. static void output_rm_filenames( struct strarray array )
  476. {
  477. static const unsigned int max_cmdline = 30000; /* to be on the safe side */
  478. unsigned int i, len;
  479. if (!array.count) return;
  480. output( "\trm -f" );
  481. for (i = len = 0; i < array.count; i++)
  482. {
  483. if (len > max_cmdline)
  484. {
  485. output( "\n" );
  486. output( "\trm -f" );
  487. len = 0;
  488. }
  489. output_filename( array.str[i] );
  490. len += strlen( array.str[i] ) + 1;
  491. }
  492. output( "\n" );
  493. }
  494. /*******************************************************************
  495. * get_extension
  496. */
  497. static char *get_extension( char *filename )
  498. {
  499. char *ext = strrchr( filename, '.' );
  500. if (ext && strchr( ext, '/' )) ext = NULL;
  501. return ext;
  502. }
  503. /*******************************************************************
  504. * get_base_name
  505. */
  506. static const char *get_base_name( const char *name )
  507. {
  508. char *base;
  509. if (!strchr( name, '.' )) return name;
  510. base = strdup( name );
  511. *strrchr( base, '.' ) = 0;
  512. return base;
  513. }
  514. /*******************************************************************
  515. * replace_extension
  516. */
  517. static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
  518. {
  519. char *ret;
  520. size_t name_len = strlen( name );
  521. size_t ext_len = strlen( old_ext );
  522. if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
  523. ret = xmalloc( name_len + strlen( new_ext ) + 1 );
  524. memcpy( ret, name, name_len );
  525. strcpy( ret + name_len, new_ext );
  526. return ret;
  527. }
  528. /*******************************************************************
  529. * replace_filename
  530. */
  531. static char *replace_filename( const char *path, const char *name )
  532. {
  533. const char *p;
  534. char *ret;
  535. size_t len;
  536. if (!path) return xstrdup( name );
  537. if (!(p = strrchr( path, '/' ))) return xstrdup( name );
  538. len = p - path + 1;
  539. ret = xmalloc( len + strlen( name ) + 1 );
  540. memcpy( ret, path, len );
  541. strcpy( ret + len, name );
  542. return ret;
  543. }
  544. /*******************************************************************
  545. * strarray_replace_extension
  546. */
  547. static struct strarray strarray_replace_extension( const struct strarray *array,
  548. const char *old_ext, const char *new_ext )
  549. {
  550. unsigned int i;
  551. struct strarray ret;
  552. ret.count = ret.size = array->count;
  553. ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
  554. for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
  555. return ret;
  556. }
  557. /*******************************************************************
  558. * replace_substr
  559. */
  560. static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
  561. {
  562. size_t pos = start - str;
  563. char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
  564. memcpy( ret, str, pos );
  565. strcpy( ret + pos, replace );
  566. strcat( ret + pos, start + len );
  567. return ret;
  568. }
  569. /*******************************************************************
  570. * get_relative_path
  571. *
  572. * Determine where the destination path is located relative to the 'from' path.
  573. */
  574. static char *get_relative_path( const char *from, const char *dest )
  575. {
  576. const char *start;
  577. char *ret, *p;
  578. unsigned int dotdots = 0;
  579. /* a path of "." is equivalent to an empty path */
  580. if (!strcmp( from, "." )) from = "";
  581. for (;;)
  582. {
  583. while (*from == '/') from++;
  584. while (*dest == '/') dest++;
  585. start = dest; /* save start of next path element */
  586. if (!*from) break;
  587. while (*from && *from != '/' && *from == *dest) { from++; dest++; }
  588. if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
  589. /* count remaining elements in 'from' */
  590. do
  591. {
  592. dotdots++;
  593. while (*from && *from != '/') from++;
  594. while (*from == '/') from++;
  595. }
  596. while (*from);
  597. break;
  598. }
  599. if (!start[0] && !dotdots) return NULL; /* empty path */
  600. ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
  601. for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
  602. if (start[0]) strcpy( p, start );
  603. else p[-1] = 0; /* remove trailing slash */
  604. return ret;
  605. }
  606. /*******************************************************************
  607. * concat_paths
  608. */
  609. static char *concat_paths( const char *base, const char *path )
  610. {
  611. if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
  612. if (!path || !path[0]) return xstrdup( base );
  613. if (path[0] == '/') return xstrdup( path );
  614. return strmake( "%s/%s", base, path );
  615. }
  616. /*******************************************************************
  617. * base_dir_path
  618. */
  619. static char *base_dir_path( const struct makefile *make, const char *path )
  620. {
  621. return concat_paths( make->base_dir, path );
  622. }
  623. /*******************************************************************
  624. * obj_dir_path
  625. */
  626. static char *obj_dir_path( const struct makefile *make, const char *path )
  627. {
  628. return concat_paths( make->obj_dir, path );
  629. }
  630. /*******************************************************************
  631. * src_dir_path
  632. */
  633. static char *src_dir_path( const struct makefile *make, const char *path )
  634. {
  635. if (make->src_dir) return concat_paths( make->src_dir, path );
  636. return obj_dir_path( make, path );
  637. }
  638. /*******************************************************************
  639. * top_obj_dir_path
  640. */
  641. static char *top_obj_dir_path( const struct makefile *make, const char *path )
  642. {
  643. return concat_paths( make->top_obj_dir, path );
  644. }
  645. /*******************************************************************
  646. * top_src_dir_path
  647. */
  648. static char *top_src_dir_path( const struct makefile *make, const char *path )
  649. {
  650. if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
  651. return top_obj_dir_path( make, path );
  652. }
  653. /*******************************************************************
  654. * root_dir_path
  655. */
  656. static char *root_dir_path( const char *path )
  657. {
  658. return concat_paths( root_src_dir, path );
  659. }
  660. /*******************************************************************
  661. * tools_dir_path
  662. */
  663. static char *tools_dir_path( const struct makefile *make, const char *path )
  664. {
  665. if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
  666. return top_obj_dir_path( make, strmake( "tools/%s", path ));
  667. }
  668. /*******************************************************************
  669. * tools_path
  670. */
  671. static char *tools_path( const struct makefile *make, const char *name )
  672. {
  673. return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
  674. }
  675. /*******************************************************************
  676. * get_line
  677. */
  678. static char *get_line( FILE *file )
  679. {
  680. static char *buffer;
  681. static size_t size;
  682. if (!size)
  683. {
  684. size = 1024;
  685. buffer = xmalloc( size );
  686. }
  687. if (!fgets( buffer, size, file )) return NULL;
  688. input_line++;
  689. for (;;)
  690. {
  691. char *p = buffer + strlen(buffer);
  692. /* if line is larger than buffer, resize buffer */
  693. while (p == buffer + size - 1 && p[-1] != '\n')
  694. {
  695. buffer = xrealloc( buffer, size * 2 );
  696. if (!fgets( buffer + size - 1, size + 1, file )) break;
  697. p = buffer + strlen(buffer);
  698. size *= 2;
  699. }
  700. if (p > buffer && p[-1] == '\n')
  701. {
  702. *(--p) = 0;
  703. if (p > buffer && p[-1] == '\r') *(--p) = 0;
  704. if (p > buffer && p[-1] == '\\')
  705. {
  706. *(--p) = 0;
  707. /* line ends in backslash, read continuation line */
  708. if (!fgets( p, size - (p - buffer), file )) return buffer;
  709. input_line++;
  710. continue;
  711. }
  712. }
  713. return buffer;
  714. }
  715. }
  716. /*******************************************************************
  717. * hash_filename
  718. */
  719. static unsigned int hash_filename( const char *name )
  720. {
  721. /* FNV-1 hash */
  722. unsigned int ret = 2166136261u;
  723. while (*name) ret = (ret * 16777619) ^ *name++;
  724. return ret % HASH_SIZE;
  725. }
  726. /*******************************************************************
  727. * add_file
  728. */
  729. static struct file *add_file( const char *name )
  730. {
  731. struct file *file = xmalloc( sizeof(*file) );
  732. memset( file, 0, sizeof(*file) );
  733. file->name = xstrdup( name );
  734. return file;
  735. }
  736. /*******************************************************************
  737. * add_dependency
  738. */
  739. static void add_dependency( struct file *file, const char *name, enum incl_type type )
  740. {
  741. /* enforce some rules for the Wine tree */
  742. if (!memcmp( name, "../", 3 ))
  743. fatal_error( "#include directive with relative path not allowed\n" );
  744. if (!strcmp( name, "config.h" ))
  745. {
  746. if (strendswith( file->name, ".h" ))
  747. fatal_error( "config.h must not be included by a header file\n" );
  748. if (file->deps_count)
  749. fatal_error( "config.h must be included before anything else\n" );
  750. }
  751. else if (!strcmp( name, "wine/port.h" ))
  752. {
  753. if (strendswith( file->name, ".h" ))
  754. fatal_error( "wine/port.h must not be included by a header file\n" );
  755. if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
  756. if (file->deps_count > 1)
  757. fatal_error( "wine/port.h must be included before everything except config.h\n" );
  758. if (strcmp( file->deps[0].name, "config.h" ))
  759. fatal_error( "config.h must be included before wine/port.h\n" );
  760. }
  761. if (file->deps_count >= file->deps_size)
  762. {
  763. file->deps_size *= 2;
  764. if (file->deps_size < 16) file->deps_size = 16;
  765. file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
  766. }
  767. file->deps[file->deps_count].line = input_line;
  768. file->deps[file->deps_count].type = type;
  769. file->deps[file->deps_count].name = xstrdup( name );
  770. file->deps_count++;
  771. }
  772. /*******************************************************************
  773. * find_src_file
  774. */
  775. static struct incl_file *find_src_file( const struct makefile *make, const char *name )
  776. {
  777. struct incl_file *file;
  778. LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
  779. if (!strcmp( name, file->name )) return file;
  780. return NULL;
  781. }
  782. /*******************************************************************
  783. * find_include_file
  784. */
  785. static struct incl_file *find_include_file( const struct makefile *make, const char *name )
  786. {
  787. struct incl_file *file;
  788. LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
  789. if (!strcmp( name, file->name )) return file;
  790. return NULL;
  791. }
  792. /*******************************************************************
  793. * add_include
  794. *
  795. * Add an include file if it doesn't already exists.
  796. */
  797. static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
  798. const char *name, int line, enum incl_type type )
  799. {
  800. struct incl_file *include;
  801. if (parent->files_count >= parent->files_size)
  802. {
  803. parent->files_size *= 2;
  804. if (parent->files_size < 16) parent->files_size = 16;
  805. parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
  806. }
  807. LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
  808. if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
  809. goto found;
  810. include = xmalloc( sizeof(*include) );
  811. memset( include, 0, sizeof(*include) );
  812. include->name = xstrdup(name);
  813. include->included_by = parent;
  814. include->included_line = line;
  815. include->type = type;
  816. include->use_msvcrt = parent->use_msvcrt;
  817. list_add_tail( &make->includes, &include->entry );
  818. found:
  819. parent->files[parent->files_count++] = include;
  820. return include;
  821. }
  822. /*******************************************************************
  823. * add_generated_source
  824. *
  825. * Add a generated source file to the list.
  826. */
  827. static struct incl_file *add_generated_source( struct makefile *make,
  828. const char *name, const char *filename )
  829. {
  830. struct incl_file *file;
  831. if ((file = find_src_file( make, name ))) return file; /* we already have it */
  832. file = xmalloc( sizeof(*file) );
  833. memset( file, 0, sizeof(*file) );
  834. file->file = add_file( name );
  835. file->name = xstrdup( name );
  836. file->filename = obj_dir_path( make, filename ? filename : name );
  837. file->file->flags = FLAG_GENERATED;
  838. file->use_msvcrt = make->use_msvcrt;
  839. list_add_tail( &make->sources, &file->entry );
  840. return file;
  841. }
  842. /*******************************************************************
  843. * parse_include_directive
  844. */
  845. static void parse_include_directive( struct file *source, char *str )
  846. {
  847. char quote, *include, *p = str;
  848. while (*p && isspace(*p)) p++;
  849. if (*p != '\"' && *p != '<' ) return;
  850. quote = *p++;
  851. if (quote == '<') quote = '>';
  852. include = p;
  853. while (*p && (*p != quote)) p++;
  854. if (!*p) fatal_error( "malformed include directive '%s'\n", str );
  855. *p = 0;
  856. add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
  857. }
  858. /*******************************************************************
  859. * parse_pragma_directive
  860. */
  861. static void parse_pragma_directive( struct file *source, char *str )
  862. {
  863. char *flag, *p = str;
  864. if (!isspace( *p )) return;
  865. while (*p && isspace(*p)) p++;
  866. p = strtok( p, " \t" );
  867. if (strcmp( p, "makedep" )) return;
  868. while ((flag = strtok( NULL, " \t" )))
  869. {
  870. if (!strcmp( flag, "depend" ))
  871. {
  872. while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
  873. return;
  874. }
  875. else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
  876. if (strendswith( source->name, ".idl" ))
  877. {
  878. if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
  879. else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
  880. else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
  881. else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
  882. else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
  883. else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
  884. else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
  885. else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
  886. }
  887. else if (strendswith( source->name, ".rc" ))
  888. {
  889. if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
  890. }
  891. else if (strendswith( source->name, ".sfd" ))
  892. {
  893. if (!strcmp( flag, "font" ))
  894. {
  895. struct strarray *array = source->args;
  896. if (!array)
  897. {
  898. source->args = array = xmalloc( sizeof(*array) );
  899. *array = empty_strarray;
  900. source->flags |= FLAG_SFD_FONTS;
  901. }
  902. strarray_add( array, xstrdup( strtok( NULL, "" )));
  903. return;
  904. }
  905. }
  906. else
  907. {
  908. if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
  909. if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
  910. }
  911. }
  912. }
  913. /*******************************************************************
  914. * parse_cpp_directive
  915. */
  916. static void parse_cpp_directive( struct file *source, char *str )
  917. {
  918. while (*str && isspace(*str)) str++;
  919. if (*str++ != '#') return;
  920. while (*str && isspace(*str)) str++;
  921. if (!strncmp( str, "include", 7 ))
  922. parse_include_directive( source, str + 7 );
  923. else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
  924. parse_include_directive( source, str + 6 );
  925. else if (!strncmp( str, "pragma", 6 ))
  926. parse_pragma_directive( source, str + 6 );
  927. }
  928. /*******************************************************************
  929. * parse_idl_file
  930. */
  931. static void parse_idl_file( struct file *source, FILE *file )
  932. {
  933. char *buffer, *include;
  934. input_line = 0;
  935. while ((buffer = get_line( file )))
  936. {
  937. char quote;
  938. char *p = buffer;
  939. while (*p && isspace(*p)) p++;
  940. if (!strncmp( p, "importlib", 9 ))
  941. {
  942. p += 9;
  943. while (*p && isspace(*p)) p++;
  944. if (*p++ != '(') continue;
  945. while (*p && isspace(*p)) p++;
  946. if (*p++ != '"') continue;
  947. include = p;
  948. while (*p && (*p != '"')) p++;
  949. if (!*p) fatal_error( "malformed importlib directive\n" );
  950. *p = 0;
  951. add_dependency( source, include, INCL_IMPORTLIB );
  952. continue;
  953. }
  954. if (!strncmp( p, "import", 6 ))
  955. {
  956. p += 6;
  957. while (*p && isspace(*p)) p++;
  958. if (*p != '"') continue;
  959. include = ++p;
  960. while (*p && (*p != '"')) p++;
  961. if (!*p) fatal_error( "malformed import directive\n" );
  962. *p = 0;
  963. add_dependency( source, include, INCL_IMPORT );
  964. continue;
  965. }
  966. /* check for #include inside cpp_quote */
  967. if (!strncmp( p, "cpp_quote", 9 ))
  968. {
  969. p += 9;
  970. while (*p && isspace(*p)) p++;
  971. if (*p++ != '(') continue;
  972. while (*p && isspace(*p)) p++;
  973. if (*p++ != '"') continue;
  974. if (*p++ != '#') continue;
  975. while (*p && isspace(*p)) p++;
  976. if (strncmp( p, "include", 7 )) continue;
  977. p += 7;
  978. while (*p && isspace(*p)) p++;
  979. if (*p == '\\' && p[1] == '"')
  980. {
  981. p += 2;
  982. quote = '"';
  983. }
  984. else
  985. {
  986. if (*p++ != '<' ) continue;
  987. quote = '>';
  988. }
  989. include = p;
  990. while (*p && (*p != quote)) p++;
  991. if (!*p || (quote == '"' && p[-1] != '\\'))
  992. fatal_error( "malformed #include directive inside cpp_quote\n" );
  993. if (quote == '"') p--; /* remove backslash */
  994. *p = 0;
  995. add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
  996. continue;
  997. }
  998. parse_cpp_directive( source, p );
  999. }
  1000. }
  1001. /*******************************************************************
  1002. * parse_c_file
  1003. */
  1004. static void parse_c_file( struct file *source, FILE *file )
  1005. {
  1006. char *buffer;
  1007. input_line = 0;
  1008. while ((buffer = get_line( file )))
  1009. {
  1010. parse_cpp_directive( source, buffer );
  1011. }
  1012. }
  1013. /*******************************************************************
  1014. * parse_rc_file
  1015. */
  1016. static void parse_rc_file( struct file *source, FILE *file )
  1017. {
  1018. char *buffer, *include;
  1019. input_line = 0;
  1020. while ((buffer = get_line( file )))
  1021. {
  1022. char quote;
  1023. char *p = buffer;
  1024. while (*p && isspace(*p)) p++;
  1025. if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
  1026. {
  1027. p += 2;
  1028. while (*p && isspace(*p)) p++;
  1029. if (strncmp( p, "@makedep:", 9 )) continue;
  1030. p += 9;
  1031. while (*p && isspace(*p)) p++;
  1032. quote = '"';
  1033. if (*p == quote)
  1034. {
  1035. include = ++p;
  1036. while (*p && *p != quote) p++;
  1037. }
  1038. else
  1039. {
  1040. include = p;
  1041. while (*p && !isspace(*p) && *p != '*') p++;
  1042. }
  1043. if (!*p)
  1044. fatal_error( "malformed makedep comment\n" );
  1045. *p = 0;
  1046. add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
  1047. continue;
  1048. }
  1049. parse_cpp_directive( source, buffer );
  1050. }
  1051. }
  1052. /*******************************************************************
  1053. * parse_in_file
  1054. */
  1055. static void parse_in_file( struct file *source, FILE *file )
  1056. {
  1057. char *p, *buffer;
  1058. /* make sure it gets rebuilt when the version changes */
  1059. add_dependency( source, "config.h", INCL_SYSTEM );
  1060. if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
  1061. input_line = 0;
  1062. while ((buffer = get_line( file )))
  1063. {
  1064. if (strncmp( buffer, ".TH", 3 )) continue;
  1065. if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
  1066. if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
  1067. if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
  1068. source->args = xstrdup( p );
  1069. return;
  1070. }
  1071. }
  1072. /*******************************************************************
  1073. * parse_sfd_file
  1074. */
  1075. static void parse_sfd_file( struct file *source, FILE *file )
  1076. {
  1077. char *p, *eol, *buffer;
  1078. input_line = 0;
  1079. while ((buffer = get_line( file )))
  1080. {
  1081. if (strncmp( buffer, "UComments:", 10 )) continue;
  1082. p = buffer + 10;
  1083. while (*p == ' ') p++;
  1084. if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
  1085. {
  1086. p++;
  1087. buffer[strlen(buffer) - 1] = 0;
  1088. }
  1089. while ((eol = strstr( p, "+AAoA" )))
  1090. {
  1091. *eol = 0;
  1092. while (*p && isspace(*p)) p++;
  1093. if (*p++ == '#')
  1094. {
  1095. while (*p && isspace(*p)) p++;
  1096. if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
  1097. }
  1098. p = eol + 5;
  1099. }
  1100. while (*p && isspace(*p)) p++;
  1101. if (*p++ != '#') return;
  1102. while (*p && isspace(*p)) p++;
  1103. if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
  1104. return;
  1105. }
  1106. }
  1107. static const struct
  1108. {
  1109. const char *ext;
  1110. void (*parse)( struct file *file, FILE *f );
  1111. } parse_functions[] =
  1112. {
  1113. { ".c", parse_c_file },
  1114. { ".h", parse_c_file },
  1115. { ".inl", parse_c_file },
  1116. { ".l", parse_c_file },
  1117. { ".m", parse_c_file },
  1118. { ".rh", parse_c_file },
  1119. { ".x", parse_c_file },
  1120. { ".y", parse_c_file },
  1121. { ".idl", parse_idl_file },
  1122. { ".rc", parse_rc_file },
  1123. { ".in", parse_in_file },
  1124. { ".sfd", parse_sfd_file }
  1125. };
  1126. /*******************************************************************
  1127. * load_file
  1128. */
  1129. static struct file *load_file( const char *name )
  1130. {
  1131. struct file *file;
  1132. FILE *f;
  1133. unsigned int i, hash = hash_filename( name );
  1134. LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
  1135. if (!strcmp( name, file->name )) return file;
  1136. if (!(f = fopen( name, "r" ))) return NULL;
  1137. file = add_file( name );
  1138. list_add_tail( &files[hash], &file->entry );
  1139. input_file_name = file->name;
  1140. input_line = 0;
  1141. for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
  1142. {
  1143. if (!strendswith( name, parse_functions[i].ext )) continue;
  1144. parse_functions[i].parse( file, f );
  1145. break;
  1146. }
  1147. fclose( f );
  1148. input_file_name = NULL;
  1149. return file;
  1150. }
  1151. /*******************************************************************
  1152. * open_include_path_file
  1153. *
  1154. * Open a file from a directory on the include path.
  1155. */
  1156. static struct file *open_include_path_file( const struct makefile *make, const char *dir,
  1157. const char *name, char **filename )
  1158. {
  1159. char *src_path = base_dir_path( make, concat_paths( dir, name ));
  1160. struct file *ret = load_file( src_path );
  1161. if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
  1162. return ret;
  1163. }
  1164. /*******************************************************************
  1165. * open_file_same_dir
  1166. *
  1167. * Open a file in the same directory as the parent.
  1168. */
  1169. static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
  1170. {
  1171. char *src_path = replace_filename( parent->file->name, name );
  1172. struct file *ret = load_file( src_path );
  1173. if (ret) *filename = replace_filename( parent->filename, name );
  1174. free( src_path );
  1175. return ret;
  1176. }
  1177. /*******************************************************************
  1178. * open_local_file
  1179. *
  1180. * Open a file in the source directory of the makefile.
  1181. */
  1182. static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
  1183. {
  1184. char *src_path = root_dir_path( base_dir_path( make, path ));
  1185. struct file *ret = load_file( src_path );
  1186. /* if not found, try parent dir */
  1187. if (!ret && make->parent_dir)
  1188. {
  1189. free( src_path );
  1190. path = strmake( "%s/%s", make->parent_dir, path );
  1191. src_path = root_dir_path( base_dir_path( make, path ));
  1192. ret = load_file( src_path );
  1193. if (ret) ret->flags |= FLAG_PARENTDIR;
  1194. }
  1195. if (ret) *filename = src_dir_path( make, path );
  1196. free( src_path );
  1197. return ret;
  1198. }
  1199. /*******************************************************************
  1200. * open_global_file
  1201. *
  1202. * Open a file in the top-level source directory.
  1203. */
  1204. static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
  1205. {
  1206. char *src_path = root_dir_path( path );
  1207. struct file *ret = load_file( src_path );
  1208. if (ret) *filename = top_src_dir_path( make, path );
  1209. free( src_path );
  1210. return ret;
  1211. }
  1212. /*******************************************************************
  1213. * open_global_header
  1214. *
  1215. * Open a file in the global include source directory.
  1216. */
  1217. static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
  1218. {
  1219. return open_global_file( make, strmake( "include/%s", path ), filename );
  1220. }
  1221. /*******************************************************************
  1222. * open_src_file
  1223. */
  1224. static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
  1225. {
  1226. struct file *file = open_local_file( make, pFile->name, &pFile->filename );
  1227. if (!file) fatal_perror( "open %s", pFile->name );
  1228. return file;
  1229. }
  1230. /*******************************************************************
  1231. * open_include_file
  1232. */
  1233. static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
  1234. {
  1235. struct file *file = NULL;
  1236. char *filename;
  1237. unsigned int i, len;
  1238. errno = ENOENT;
  1239. /* check for generated bison header */
  1240. if (strendswith( pFile->name, ".tab.h" ) &&
  1241. (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
  1242. {
  1243. pFile->sourcename = filename;
  1244. pFile->filename = obj_dir_path( make, pFile->name );
  1245. return file;
  1246. }
  1247. /* check for corresponding idl file in source dir */
  1248. if (strendswith( pFile->name, ".h" ) &&
  1249. (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
  1250. {
  1251. pFile->sourcename = filename;
  1252. pFile->filename = obj_dir_path( make, pFile->name );
  1253. return file;
  1254. }
  1255. /* check for corresponding tlb file in source dir */
  1256. if (strendswith( pFile->name, ".tlb" ) &&
  1257. (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
  1258. {
  1259. pFile->sourcename = filename;
  1260. pFile->filename = obj_dir_path( make, pFile->name );
  1261. return file;
  1262. }
  1263. /* check for extra targets */
  1264. if (strarray_exists( &make->extra_targets, pFile->name ))
  1265. {
  1266. pFile->sourcename = filename;
  1267. pFile->filename = obj_dir_path( make, pFile->name );
  1268. return NULL;
  1269. }
  1270. /* now try in source dir */
  1271. if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
  1272. /* check for corresponding idl file in global includes */
  1273. if (strendswith( pFile->name, ".h" ) &&
  1274. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
  1275. {
  1276. pFile->sourcename = filename;
  1277. pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
  1278. return file;
  1279. }
  1280. /* check for corresponding .in file in global includes (for config.h.in) */
  1281. if (strendswith( pFile->name, ".h" ) &&
  1282. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
  1283. {
  1284. pFile->sourcename = filename;
  1285. pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
  1286. return file;
  1287. }
  1288. /* check for corresponding .x file in global includes */
  1289. if (strendswith( pFile->name, "tmpl.h" ) &&
  1290. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
  1291. {
  1292. pFile->sourcename = filename;
  1293. pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
  1294. return file;
  1295. }
  1296. /* check for corresponding .tlb file in global includes */
  1297. if (strendswith( pFile->name, ".tlb" ) &&
  1298. (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
  1299. {
  1300. pFile->sourcename = filename;
  1301. pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
  1302. return file;
  1303. }
  1304. /* check in global includes source dir */
  1305. if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
  1306. /* check in global msvcrt includes */
  1307. if (pFile->use_msvcrt &&
  1308. (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
  1309. return file;
  1310. /* now search in include paths */
  1311. for (i = 0; i < make->include_paths.count; i++)
  1312. {
  1313. const char *dir = make->include_paths.str[i];
  1314. const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
  1315. if (prefix)
  1316. {
  1317. len = strlen( prefix );
  1318. if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
  1319. {
  1320. while (dir[len] == '/') len++;
  1321. file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
  1322. if (file) return file;
  1323. }
  1324. if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
  1325. }
  1326. if (*dir != '/')
  1327. {
  1328. if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
  1329. return file;
  1330. }
  1331. }
  1332. if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt)
  1333. {
  1334. if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
  1335. fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
  1336. pFile->included_by->file->name, pFile->included_line, pFile->name );
  1337. exit(1);
  1338. }
  1339. if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
  1340. /* try in src file directory */
  1341. if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
  1342. fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
  1343. perror( pFile->name );
  1344. pFile = pFile->included_by;
  1345. while (pFile && pFile->included_by)
  1346. {
  1347. const char *parent = pFile->included_by->sourcename;
  1348. if (!parent) parent = pFile->included_by->file->name;
  1349. fprintf( stderr, "%s:%d: note: %s was first included here\n",
  1350. parent, pFile->included_line, pFile->name );
  1351. pFile = pFile->included_by;
  1352. }
  1353. exit(1);
  1354. }
  1355. /*******************************************************************
  1356. * add_all_includes
  1357. */
  1358. static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
  1359. {
  1360. unsigned int i;
  1361. parent->files_count = 0;
  1362. parent->files_size = file->deps_count;
  1363. parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
  1364. for (i = 0; i < file->deps_count; i++)
  1365. {
  1366. switch (file->deps[i].type)
  1367. {
  1368. case INCL_NORMAL:
  1369. case INCL_IMPORT:
  1370. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1371. break;
  1372. case INCL_IMPORTLIB:
  1373. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
  1374. break;
  1375. case INCL_SYSTEM:
  1376. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
  1377. break;
  1378. case INCL_CPP_QUOTE:
  1379. case INCL_CPP_QUOTE_SYSTEM:
  1380. break;
  1381. }
  1382. }
  1383. }
  1384. /*******************************************************************
  1385. * parse_file
  1386. */
  1387. static void parse_file( struct makefile *make, struct incl_file *source, int src )
  1388. {
  1389. struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
  1390. if (!file) return;
  1391. source->file = file;
  1392. source->files_count = 0;
  1393. source->files_size = file->deps_count;
  1394. source->files = xmalloc( source->files_size * sizeof(*source->files) );
  1395. if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
  1396. else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
  1397. if (source->sourcename)
  1398. {
  1399. if (strendswith( source->sourcename, ".idl" ))
  1400. {
  1401. unsigned int i;
  1402. if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
  1403. /* generated .h file always includes these */
  1404. add_include( make, source, "rpc.h", 0, INCL_NORMAL );
  1405. add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
  1406. for (i = 0; i < file->deps_count; i++)
  1407. {
  1408. switch (file->deps[i].type)
  1409. {
  1410. case INCL_IMPORT:
  1411. if (strendswith( file->deps[i].name, ".idl" ))
  1412. add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
  1413. file->deps[i].line, INCL_NORMAL );
  1414. else
  1415. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1416. break;
  1417. case INCL_CPP_QUOTE:
  1418. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1419. break;
  1420. case INCL_CPP_QUOTE_SYSTEM:
  1421. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
  1422. break;
  1423. case INCL_NORMAL:
  1424. case INCL_SYSTEM:
  1425. case INCL_IMPORTLIB:
  1426. break;
  1427. }
  1428. }
  1429. return;
  1430. }
  1431. if (strendswith( source->sourcename, ".y" ))
  1432. return; /* generated .tab.h doesn't include anything */
  1433. }
  1434. add_all_includes( make, source, file );
  1435. }
  1436. /*******************************************************************
  1437. * add_src_file
  1438. *
  1439. * Add a source file to the list.
  1440. */
  1441. static struct incl_file *add_src_file( struct makefile *make, const char *name )
  1442. {
  1443. struct incl_file *file;
  1444. if ((file = find_src_file( make, name ))) return file; /* we already have it */
  1445. file = xmalloc( sizeof(*file) );
  1446. memset( file, 0, sizeof(*file) );
  1447. file->name = xstrdup(name);
  1448. file->use_msvcrt = make->use_msvcrt;
  1449. list_add_tail( &make->sources, &file->entry );
  1450. parse_file( make, file, 1 );
  1451. return file;
  1452. }
  1453. /*******************************************************************
  1454. * open_input_makefile
  1455. */
  1456. static FILE *open_input_makefile( const struct makefile *make )
  1457. {
  1458. FILE *ret;
  1459. if (make->base_dir)
  1460. input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
  1461. else
  1462. input_file_name = output_makefile_name; /* always use output name for main Makefile */
  1463. input_line = 0;
  1464. if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
  1465. return ret;
  1466. }
  1467. /*******************************************************************
  1468. * get_make_variable
  1469. */
  1470. static const char *get_make_variable( const struct makefile *make, const char *name )
  1471. {
  1472. const char *ret;
  1473. if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
  1474. if ((ret = strarray_get_value( &make->vars, name ))) return ret;
  1475. if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
  1476. return NULL;
  1477. }
  1478. /*******************************************************************
  1479. * get_expanded_make_variable
  1480. */
  1481. static char *get_expanded_make_variable( const struct makefile *make, const char *name )
  1482. {
  1483. const char *var;
  1484. char *p, *end, *expand, *tmp;
  1485. var = get_make_variable( make, name );
  1486. if (!var) return NULL;
  1487. p = expand = xstrdup( var );
  1488. while ((p = strchr( p, '$' )))
  1489. {
  1490. if (p[1] == '(')
  1491. {
  1492. if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
  1493. *end++ = 0;
  1494. if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
  1495. var = get_make_variable( make, p + 2 );
  1496. tmp = replace_substr( expand, p, end - p, var ? var : "" );
  1497. /* switch to the new string */
  1498. p = tmp + (p - expand);
  1499. free( expand );
  1500. expand = tmp;
  1501. }
  1502. else if (p[1] == '{') /* don't expand ${} variables */
  1503. {
  1504. if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
  1505. p = end + 1;
  1506. }
  1507. else if (p[1] == '$')
  1508. {
  1509. p += 2;
  1510. }
  1511. else fatal_error( "syntax error in '%s'\n", expand );
  1512. }
  1513. /* consider empty variables undefined */
  1514. p = expand;
  1515. while (*p && isspace(*p)) p++;
  1516. if (*p) return expand;
  1517. free( expand );
  1518. return NULL;
  1519. }
  1520. /*******************************************************************
  1521. * get_expanded_make_var_array
  1522. */
  1523. static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
  1524. {
  1525. struct strarray ret = empty_strarray;
  1526. char *value, *token;
  1527. if ((value = get_expanded_make_variable( make, name )))
  1528. for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
  1529. strarray_add( &ret, token );
  1530. return ret;
  1531. }
  1532. /*******************************************************************
  1533. * get_expanded_file_local_var
  1534. */
  1535. static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
  1536. const char *name )
  1537. {
  1538. char *p, *var = strmake( "%s_%s", file, name );
  1539. for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
  1540. return get_expanded_make_var_array( make, var );
  1541. }
  1542. /*******************************************************************
  1543. * set_make_variable
  1544. */
  1545. static int set_make_variable( struct strarray *array, const char *assignment )
  1546. {
  1547. char *p, *name;
  1548. p = name = xstrdup( assignment );
  1549. while (isalnum(*p) || *p == '_') p++;
  1550. if (name == p) return 0; /* not a variable */
  1551. if (isspace(*p))
  1552. {
  1553. *p++ = 0;
  1554. while (isspace(*p)) p++;
  1555. }
  1556. if (*p != '=') return 0; /* not an assignment */
  1557. *p++ = 0;
  1558. while (isspace(*p)) p++;
  1559. strarray_set_value( array, name, p );
  1560. return 1;
  1561. }
  1562. /*******************************************************************
  1563. * parse_makefile
  1564. */
  1565. static struct makefile *parse_makefile( const char *path )
  1566. {
  1567. char *buffer;
  1568. FILE *file;
  1569. struct makefile *make = xmalloc( sizeof(*make) );
  1570. memset( make, 0, sizeof(*make) );
  1571. if (path)
  1572. {
  1573. make->top_obj_dir = get_relative_path( path, "" );
  1574. make->base_dir = path;
  1575. if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
  1576. }
  1577. file = open_input_makefile( make );
  1578. while ((buffer = get_line( file )))
  1579. {
  1580. if (!strncmp( buffer, separator, strlen(separator) )) break;
  1581. if (*buffer == '\t') continue; /* command */
  1582. while (isspace( *buffer )) buffer++;
  1583. if (*buffer == '#') continue; /* comment */
  1584. set_make_variable( &make->vars, buffer );
  1585. }
  1586. fclose( file );
  1587. input_file_name = NULL;
  1588. return make;
  1589. }
  1590. /*******************************************************************
  1591. * add_generated_sources
  1592. */
  1593. static void add_generated_sources( struct makefile *make )
  1594. {
  1595. unsigned int i;
  1596. struct incl_file *source, *next, *file;
  1597. struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
  1598. LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
  1599. {
  1600. if (source->file->flags & FLAG_IDL_CLIENT)
  1601. {
  1602. file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
  1603. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1604. add_all_includes( make, file, file->file );
  1605. }
  1606. if (source->file->flags & FLAG_IDL_SERVER)
  1607. {
  1608. file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
  1609. add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
  1610. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1611. add_all_includes( make, file, file->file );
  1612. }
  1613. if (source->file->flags & FLAG_IDL_IDENT)
  1614. {
  1615. file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
  1616. add_dependency( file->file, "rpc.h", INCL_NORMAL );
  1617. add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
  1618. add_dependency( file->file, "guiddef.h", INCL_NORMAL );
  1619. add_all_includes( make, file, file->file );
  1620. }
  1621. if (source->file->flags & FLAG_IDL_PROXY)
  1622. {
  1623. file = add_generated_source( make, "dlldata.o", "dlldata.c" );
  1624. add_dependency( file->file, "objbase.h", INCL_NORMAL );
  1625. add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
  1626. add_all_includes( make, file, file->file );
  1627. file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
  1628. add_dependency( file->file, "objbase.h", INCL_NORMAL );
  1629. add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
  1630. add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
  1631. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1632. add_all_includes( make, file, file->file );
  1633. }
  1634. if (source->file->flags & FLAG_IDL_TYPELIB)
  1635. {
  1636. add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
  1637. }
  1638. if (source->file->flags & FLAG_IDL_REGTYPELIB)
  1639. {
  1640. add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
  1641. }
  1642. if (source->file->flags & FLAG_IDL_REGISTER)
  1643. {
  1644. add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
  1645. }
  1646. if (source->file->flags & FLAG_IDL_HEADER)
  1647. {
  1648. add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
  1649. }
  1650. if (!source->file->flags && strendswith( source->name, ".idl" ))
  1651. {
  1652. add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
  1653. }
  1654. if (strendswith( source->name, ".x" ))
  1655. {
  1656. add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
  1657. }
  1658. if (strendswith( source->name, ".y" ))
  1659. {
  1660. file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
  1661. /* steal the includes list from the source file */
  1662. file->files_count = source->files_count;
  1663. file->files_size = source->files_size;
  1664. file->files = source->files;
  1665. source->files_count = source->files_size = 0;
  1666. source->files = NULL;
  1667. }
  1668. if (strendswith( source->name, ".l" ))
  1669. {
  1670. file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
  1671. /* steal the includes list from the source file */
  1672. file->files_count = source->files_count;
  1673. file->files_size = source->files_size;
  1674. file->files = source->files;
  1675. source->files_count = source->files_size = 0;
  1676. source->files = NULL;
  1677. }
  1678. if (source->file->flags & FLAG_C_IMPLIB)
  1679. {
  1680. if (!make->staticimplib && make->importlib && *dll_ext)
  1681. make->staticimplib = strmake( "lib%s.a", make->importlib );
  1682. }
  1683. if (strendswith( source->name, ".po" ))
  1684. {
  1685. if (!make->disabled)
  1686. strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
  1687. }
  1688. }
  1689. if (make->testdll)
  1690. {
  1691. file = add_generated_source( make, "testlist.o", "testlist.c" );
  1692. add_dependency( file->file, "wine/test.h", INCL_NORMAL );
  1693. add_all_includes( make, file, file->file );
  1694. }
  1695. for (i = 0; i < objs.count; i++)
  1696. {
  1697. /* default to .c for unknown extra object files */
  1698. if (strendswith( objs.str[i], ".o" ))
  1699. add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
  1700. else if (strendswith( objs.str[i], ".res" ))
  1701. add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
  1702. else
  1703. add_generated_source( make, objs.str[i], NULL );
  1704. }
  1705. }
  1706. /*******************************************************************
  1707. * create_dir
  1708. */
  1709. static void create_dir( const char *dir )
  1710. {
  1711. char *p, *path;
  1712. p = path = xstrdup( dir );
  1713. while ((p = strchr( p, '/' )))
  1714. {
  1715. *p = 0;
  1716. if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
  1717. *p++ = '/';
  1718. while (*p == '/') p++;
  1719. }
  1720. if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
  1721. free( path );
  1722. }
  1723. /*******************************************************************
  1724. * create_file_directories
  1725. *
  1726. * Create the base directories of all the files.
  1727. */
  1728. static void create_file_directories( const struct makefile *make, struct strarray files )
  1729. {
  1730. struct strarray subdirs = empty_strarray;
  1731. unsigned int i;
  1732. char *dir;
  1733. for (i = 0; i < files.count; i++)
  1734. {
  1735. if (!strchr( files.str[i], '/' )) continue;
  1736. dir = base_dir_path( make, files.str[i] );
  1737. *strrchr( dir, '/' ) = 0;
  1738. strarray_add_uniq( &subdirs, dir );
  1739. }
  1740. for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
  1741. }
  1742. /*******************************************************************
  1743. * output_filenames_obj_dir
  1744. */
  1745. static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
  1746. {
  1747. unsigned int i;
  1748. for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
  1749. }
  1750. /*******************************************************************
  1751. * get_dependencies
  1752. */
  1753. static void get_dependencies( struct incl_file *file, struct incl_file *source )
  1754. {
  1755. unsigned int i;
  1756. if (!file->filename) return;
  1757. if (file != source)
  1758. {
  1759. if (file->owner == source) return; /* already processed */
  1760. if (file->type == INCL_IMPORTLIB &&
  1761. !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
  1762. return; /* library is imported only when building a typelib */
  1763. file->owner = source;
  1764. strarray_add( &source->dependencies, file->filename );
  1765. }
  1766. for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
  1767. }
  1768. /*******************************************************************
  1769. * get_local_dependencies
  1770. *
  1771. * Get the local dependencies of a given target.
  1772. */
  1773. static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
  1774. struct strarray targets )
  1775. {
  1776. unsigned int i;
  1777. struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
  1778. for (i = 0; i < deps.count; i++)
  1779. {
  1780. if (strarray_exists( &targets, deps.str[i] ))
  1781. deps.str[i] = obj_dir_path( make, deps.str[i] );
  1782. else
  1783. deps.str[i] = src_dir_path( make, deps.str[i] );
  1784. }
  1785. return deps;
  1786. }
  1787. /*******************************************************************
  1788. * get_static_lib
  1789. *
  1790. * Check if makefile builds the named static library and return the full lib path.
  1791. */
  1792. static const char *get_static_lib( const struct makefile *make, const char *name )
  1793. {
  1794. if (!make->staticlib) return NULL;
  1795. if (strncmp( make->staticlib, "lib", 3 )) return NULL;
  1796. if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
  1797. if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
  1798. return base_dir_path( make, make->staticlib );
  1799. }
  1800. /*******************************************************************
  1801. * get_parent_makefile
  1802. */
  1803. static struct makefile *get_parent_makefile( struct makefile *make )
  1804. {
  1805. char *dir, *p;
  1806. int i;
  1807. if (!make->base_dir) return NULL;
  1808. dir = xstrdup( make->base_dir );
  1809. if (!(p = strrchr( dir, '/' ))) return NULL;
  1810. *p = 0;
  1811. for (i = 0; i < top_makefile->subdirs.count; i++)
  1812. if (!strcmp( top_makefile->submakes[i]->base_dir, dir )) return top_makefile->submakes[i];
  1813. return NULL;
  1814. }
  1815. /*******************************************************************
  1816. * needs_cross_lib
  1817. */
  1818. static int needs_cross_lib( const struct makefile *make )
  1819. {
  1820. const char *name;
  1821. if (!crosstarget) return 0;
  1822. if (make->importlib) name = make->importlib;
  1823. else if (make->staticlib)
  1824. {
  1825. name = replace_extension( make->staticlib, ".a", "" );
  1826. if (!strncmp( name, "lib", 3 )) name += 3;
  1827. }
  1828. else return 0;
  1829. if (strarray_exists( &cross_import_libs, name )) return 1;
  1830. if (delay_load_flag && strarray_exists( &delay_import_libs, name )) return 1;
  1831. return 0;
  1832. }
  1833. /*******************************************************************
  1834. * needs_delay_lib
  1835. */
  1836. static int needs_delay_lib( const struct makefile *make )
  1837. {
  1838. if (delay_load_flag) return 0;
  1839. if (*dll_ext && !crosstarget) return 0;
  1840. if (!make->importlib) return 0;
  1841. return strarray_exists( &delay_import_libs, make->importlib );
  1842. }
  1843. /*******************************************************************
  1844. * add_default_libraries
  1845. */
  1846. static struct strarray add_default_libraries( const struct makefile *make, struct strarray *deps )
  1847. {
  1848. struct strarray ret = empty_strarray;
  1849. struct strarray all_libs = empty_strarray;
  1850. unsigned int i, j;
  1851. if (!make->use_msvcrt)
  1852. {
  1853. strarray_add( &all_libs, "-lwine_port" );
  1854. strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
  1855. strarray_addall( &all_libs, libs );
  1856. }
  1857. for (i = 0; i < all_libs.count; i++)
  1858. {
  1859. const char *lib = NULL;
  1860. if (!strncmp( all_libs.str[i], "-l", 2 ))
  1861. {
  1862. const char *name = all_libs.str[i] + 2;
  1863. for (j = 0; j < top_makefile->subdirs.count; j++)
  1864. {
  1865. const struct makefile *submake = top_makefile->submakes[j];
  1866. if ((lib = get_static_lib( submake, name ))) break;
  1867. }
  1868. }
  1869. if (lib)
  1870. {
  1871. lib = top_obj_dir_path( make, lib );
  1872. strarray_add( deps, lib );
  1873. strarray_add( &ret, lib );
  1874. }
  1875. else strarray_add( &ret, all_libs.str[i] );
  1876. }
  1877. return ret;
  1878. }
  1879. /*******************************************************************
  1880. * add_import_libs
  1881. */
  1882. static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
  1883. struct strarray imports, int delay )
  1884. {
  1885. struct strarray ret = empty_strarray;
  1886. unsigned int i, j;
  1887. for (i = 0; i < imports.count; i++)
  1888. {
  1889. const char *name = get_base_name( imports.str[i] );
  1890. const char *lib = NULL;
  1891. /* skip module's own importlib, its object files will be linked directly */
  1892. if (make->importlib && !strcmp( make->importlib, imports.str[i] )) continue;
  1893. for (j = 0; j < top_makefile->subdirs.count; j++)
  1894. {
  1895. const struct makefile *submake = top_makefile->submakes[j];
  1896. if (submake->importlib && !strcmp( submake->importlib, name ))
  1897. {
  1898. if (make->is_cross || !*dll_ext || submake->staticimplib)
  1899. lib = base_dir_path( submake, strmake( "lib%s.a", name ));
  1900. else
  1901. strarray_add( deps, top_obj_dir_path( make,
  1902. strmake( "%s/lib%s.def", submake->base_dir, name )));
  1903. break;
  1904. }
  1905. if ((lib = get_static_lib( submake, name ))) break;
  1906. }
  1907. if (lib)
  1908. {
  1909. if (delay && !delay_load_flag) lib = replace_extension( lib, ".a", ".delay.a" );
  1910. else if (make->is_cross) lib = replace_extension( lib, ".a", ".cross.a" );
  1911. lib = top_obj_dir_path( make, lib );
  1912. strarray_add( deps, lib );
  1913. strarray_add( &ret, lib );
  1914. }
  1915. else strarray_add( &ret, strmake( "-l%s", name ));
  1916. }
  1917. return ret;
  1918. }
  1919. /*******************************************************************
  1920. * get_default_imports
  1921. */
  1922. static struct strarray get_default_imports( const struct makefile *make )
  1923. {
  1924. struct strarray ret = empty_strarray;
  1925. if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
  1926. strarray_add( &ret, "winecrt0" );
  1927. if (make->is_win16) strarray_add( &ret, "kernel" );
  1928. strarray_add( &ret, "kernel32" );
  1929. strarray_add( &ret, "ntdll" );
  1930. return ret;
  1931. }
  1932. /*******************************************************************
  1933. * add_install_rule
  1934. */
  1935. static void add_install_rule( struct makefile *make, const char *target,
  1936. const char *file, const char *dest )
  1937. {
  1938. if (strarray_exists( &make->install_lib, target ) ||
  1939. strarray_exists( &top_install_lib, make->base_dir ) ||
  1940. strarray_exists( &top_install_lib, base_dir_path( make, target )))
  1941. {
  1942. strarray_add( &make->install_rules[INSTALL_LIB], file );
  1943. strarray_add( &make->install_rules[INSTALL_LIB], dest );
  1944. }
  1945. else if (strarray_exists( &make->install_dev, target ) ||
  1946. strarray_exists( &top_install_dev, make->base_dir ) ||
  1947. strarray_exists( &top_install_dev, base_dir_path( make, target )))
  1948. {
  1949. strarray_add( &make->install_rules[INSTALL_DEV], file );
  1950. strarray_add( &make->install_rules[INSTALL_DEV], dest );
  1951. }
  1952. }
  1953. /*******************************************************************
  1954. * get_include_install_path
  1955. *
  1956. * Determine the installation path for a given include file.
  1957. */
  1958. static const char *get_include_install_path( const char *name )
  1959. {
  1960. if (!strncmp( name, "wine/", 5 )) return name + 5;
  1961. if (!strncmp( name, "msvcrt/", 7 )) return name;
  1962. return strmake( "windows/%s", name );
  1963. }
  1964. /*******************************************************************
  1965. * get_shared_library_name
  1966. *
  1967. * Determine possible names for a shared library with a version number.
  1968. */
  1969. static struct strarray get_shared_lib_names( const char *libname )
  1970. {
  1971. struct strarray ret = empty_strarray;
  1972. const char *ext, *p;
  1973. char *name, *first, *second;
  1974. size_t len = 0;
  1975. strarray_add( &ret, libname );
  1976. for (p = libname; (p = strchr( p, '.' )); p++)
  1977. if ((len = strspn( p + 1, "0123456789." ))) break;
  1978. if (!len) return ret;
  1979. ext = p + 1 + len;
  1980. if (*ext && ext[-1] == '.') ext--;
  1981. /* keep only the first group of digits */
  1982. name = xstrdup( libname );
  1983. first = name + (p - libname);
  1984. if ((second = strchr( first + 1, '.' )))
  1985. {
  1986. strcpy( second, ext );
  1987. strarray_add( &ret, xstrdup( name ));
  1988. }
  1989. /* now remove all digits */
  1990. strcpy( first, ext );
  1991. strarray_add( &ret, name );
  1992. return ret;
  1993. }
  1994. /*******************************************************************
  1995. * get_source_defines
  1996. */
  1997. static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
  1998. const char *obj )
  1999. {
  2000. unsigned int i;
  2001. struct strarray ret = empty_strarray;
  2002. strarray_addall( &ret, make->include_args );
  2003. if (source->use_msvcrt)
  2004. strarray_add( &ret, strmake( "-I%s", top_src_dir_path( make, "include/msvcrt" )));
  2005. for (i = 0; i < make->include_paths.count; i++)
  2006. strarray_add( &ret, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
  2007. strarray_addall( &ret, make->define_args );
  2008. strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
  2009. return ret;
  2010. }
  2011. /*******************************************************************
  2012. * output_winegcc_command
  2013. */
  2014. static void output_winegcc_command( struct makefile *make )
  2015. {
  2016. output( "\t%s -o $@", tools_path( make, "winegcc" ));
  2017. output_filename( "--wine-objdir" );
  2018. output_filename( top_obj_dir_path( make, "" ));
  2019. if (tools_dir)
  2020. {
  2021. output_filename( "--winebuild" );
  2022. output_filename( tools_path( make, "winebuild" ));
  2023. }
  2024. if (make->is_cross)
  2025. {
  2026. output_filename( "-b" );
  2027. output_filename( crosstarget );
  2028. output_filename( "--lib-suffix=.cross.a" );
  2029. }
  2030. else
  2031. {
  2032. output_filenames( target_flags );
  2033. output_filenames( lddll_flags );
  2034. }
  2035. }
  2036. /*******************************************************************
  2037. * output_symlink_rule
  2038. *
  2039. * Output a rule to create a symlink.
  2040. */
  2041. static void output_symlink_rule( const char *src_name, const char *link_name )
  2042. {
  2043. const char *name;
  2044. output( "\trm -f %s && ", link_name );
  2045. /* dest path with a directory needs special handling if ln -s isn't supported */
  2046. if (strcmp( ln_s, "ln -s" ) && ((name = strrchr( link_name, '/' ))))
  2047. {
  2048. char *dir = xstrdup( link_name );
  2049. dir[name - link_name] = 0;
  2050. output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
  2051. free( dir );
  2052. }
  2053. else
  2054. {
  2055. output( "%s %s %s\n", ln_s, src_name, link_name );
  2056. }
  2057. }
  2058. /*******************************************************************
  2059. * output_srcdir_symlink
  2060. *
  2061. * Output rule to create a symlink back to the source directory, for source files
  2062. * that are needed at run-time.
  2063. */
  2064. static void output_srcdir_symlink( struct makefile *make, const char *obj )
  2065. {
  2066. char *src_file;
  2067. if (!make->src_dir) return;
  2068. src_file = src_dir_path( make, obj );
  2069. output( "%s: %s\n", obj, src_file );
  2070. output_symlink_rule( src_file, obj );
  2071. strarray_add( &make->all_targets, obj );
  2072. }
  2073. /*******************************************************************
  2074. * output_install_commands
  2075. */
  2076. static void output_install_commands( struct makefile *make, const struct makefile *submake,
  2077. struct strarray files )
  2078. {
  2079. unsigned int i;
  2080. char *install_sh = top_src_dir_path( make, "tools/install-sh" );
  2081. for (i = 0; i < files.count; i += 2)
  2082. {
  2083. const char *file = files.str[i];
  2084. const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
  2085. if (submake) file = base_dir_path( submake, file );
  2086. switch (*files.str[i + 1])
  2087. {
  2088. case 'c': /* cross-compiled program */
  2089. output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
  2090. crosstarget, install_sh, obj_dir_path( make, file ), dest );
  2091. output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
  2092. break;
  2093. case 'd': /* data file */
  2094. output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
  2095. install_sh, obj_dir_path( make, file ), dest );
  2096. break;
  2097. case 'D': /* data file in source dir */
  2098. output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
  2099. install_sh, src_dir_path( make, file ), dest );
  2100. break;
  2101. case 'p': /* program file */
  2102. output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
  2103. install_sh, obj_dir_path( make, file ), dest );
  2104. break;
  2105. case 's': /* script */
  2106. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2107. install_sh, obj_dir_path( make, file ), dest );
  2108. break;
  2109. case 'S': /* script in source dir */
  2110. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2111. install_sh, src_dir_path( make, file ), dest );
  2112. break;
  2113. case 't': /* script in tools dir */
  2114. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2115. install_sh, tools_dir_path( make, files.str[i] ), dest );
  2116. break;
  2117. case 'y': /* symlink */
  2118. output_symlink_rule( files.str[i], dest );
  2119. break;
  2120. default:
  2121. assert(0);
  2122. }
  2123. strarray_add( &make->uninstall_files, dest );
  2124. }
  2125. }
  2126. /*******************************************************************
  2127. * output_install_rules
  2128. *
  2129. * Rules are stored as a (file,dest) pair of values.
  2130. * The first char of dest indicates the type of install.
  2131. */
  2132. static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
  2133. {
  2134. unsigned int i;
  2135. struct strarray files = make->install_rules[rules];
  2136. struct strarray targets = empty_strarray;
  2137. if (!files.count) return;
  2138. for (i = 0; i < files.count; i += 2)
  2139. {
  2140. const char *file = files.str[i];
  2141. switch (*files.str[i + 1])
  2142. {
  2143. case 'c': /* cross-compiled program */
  2144. case 'd': /* data file */
  2145. case 'p': /* program file */
  2146. case 's': /* script */
  2147. strarray_add_uniq( &targets, obj_dir_path( make, file ));
  2148. break;
  2149. case 't': /* script in tools dir */
  2150. strarray_add_uniq( &targets, tools_dir_path( make, file ));
  2151. break;
  2152. }
  2153. }
  2154. output( "install %s::", target );
  2155. output_filenames( targets );
  2156. output( "\n" );
  2157. output_install_commands( make, NULL, files );
  2158. strarray_add_uniq( &make->phony_targets, "install" );
  2159. strarray_add_uniq( &make->phony_targets, target );
  2160. }
  2161. static int cmp_string_length( const char **a, const char **b )
  2162. {
  2163. int paths_a = 0, paths_b = 0;
  2164. const char *p;
  2165. for (p = *a; *p; p++) if (*p == '/') paths_a++;
  2166. for (p = *b; *p; p++) if (*p == '/') paths_b++;
  2167. if (paths_b != paths_a) return paths_b - paths_a;
  2168. return strcmp( *a, *b );
  2169. }
  2170. /*******************************************************************
  2171. * output_uninstall_rules
  2172. */
  2173. static void output_uninstall_rules( struct makefile *make )
  2174. {
  2175. static const char *dirs_order[] =
  2176. { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };
  2177. struct strarray subdirs = empty_strarray;
  2178. unsigned int i, j;
  2179. if (!make->uninstall_files.count) return;
  2180. output( "uninstall::\n" );
  2181. output_rm_filenames( make->uninstall_files );
  2182. strarray_add_uniq( &make->phony_targets, "uninstall" );
  2183. if (!make->subdirs.count) return;
  2184. for (i = 0; i < make->uninstall_files.count; i++)
  2185. {
  2186. char *dir = xstrdup( make->uninstall_files.str[i] );
  2187. while (strchr( dir, '/' ))
  2188. {
  2189. *strrchr( dir, '/' ) = 0;
  2190. strarray_add_uniq( &subdirs, xstrdup(dir) );
  2191. }
  2192. }
  2193. strarray_qsort( &subdirs, cmp_string_length );
  2194. output( "\t-rmdir" );
  2195. for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
  2196. {
  2197. for (j = 0; j < subdirs.count; j++)
  2198. {
  2199. if (!subdirs.str[j]) continue;
  2200. if (strncmp( subdirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
  2201. continue;
  2202. output_filename( subdirs.str[j] );
  2203. subdirs.str[j] = NULL;
  2204. }
  2205. }
  2206. for (j = 0; j < subdirs.count; j++)
  2207. if (subdirs.str[j]) output_filename( subdirs.str[j] );
  2208. output( "\n" );
  2209. }
  2210. /*******************************************************************
  2211. * output_importlib_symlinks
  2212. */
  2213. static struct strarray output_importlib_symlinks( const struct makefile *parent,
  2214. const struct makefile *make )
  2215. {
  2216. struct strarray ret = empty_strarray;
  2217. const char *lib, *dst, *ext[4];
  2218. int i, count = 0;
  2219. if (!make->module) return ret;
  2220. if (!make->importlib) return ret;
  2221. if (make->is_win16 && make->disabled) return ret;
  2222. if (strncmp( make->base_dir, "dlls/", 5 )) return ret;
  2223. if (!strcmp( make->module, make->importlib )) return ret;
  2224. if (!strchr( make->importlib, '.' ) &&
  2225. !strncmp( make->module, make->importlib, strlen( make->importlib )) &&
  2226. !strcmp( make->module + strlen( make->importlib ), ".dll" ))
  2227. return ret;
  2228. ext[count++] = *dll_ext ? "def" : "a";
  2229. if (needs_delay_lib( make )) ext[count++] = "delay.a";
  2230. if (needs_cross_lib( make )) ext[count++] = "cross.a";
  2231. for (i = 0; i < count; i++)
  2232. {
  2233. lib = strmake( "lib%s.%s", make->importlib, ext[i] );
  2234. dst = concat_paths( obj_dir_path( parent, "dlls" ), lib );
  2235. output( "%s: %s\n", dst, base_dir_path( make, lib ));
  2236. output_symlink_rule( concat_paths( make->base_dir + strlen("dlls/"), lib ), dst );
  2237. strarray_add( &ret, dst );
  2238. }
  2239. return ret;
  2240. }
  2241. /*******************************************************************
  2242. * output_po_files
  2243. */
  2244. static void output_po_files( const struct makefile *make )
  2245. {
  2246. const char *po_dir = src_dir_path( make, "po" );
  2247. struct strarray pot_files = empty_strarray;
  2248. struct incl_file *source;
  2249. unsigned int i;
  2250. for (i = 0; i < make->subdirs.count; i++)
  2251. {
  2252. struct makefile *submake = make->submakes[i];
  2253. LIST_FOR_EACH_ENTRY( source, &submake->sources, struct incl_file, entry )
  2254. {
  2255. if (source->file->flags & FLAG_PARENTDIR) continue;
  2256. if (strendswith( source->name, ".rc" ) && (source->file->flags & FLAG_RC_PO))
  2257. {
  2258. char *pot_file = replace_extension( source->name, ".rc", ".pot" );
  2259. char *pot_path = base_dir_path( submake, pot_file );
  2260. output( "%s: tools/wrc include dummy\n", pot_path );
  2261. output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
  2262. strarray_add( &pot_files, pot_path );
  2263. }
  2264. else if (strendswith( source->name, ".mc" ))
  2265. {
  2266. char *pot_file = replace_extension( source->name, ".mc", ".pot" );
  2267. char *pot_path = base_dir_path( submake, pot_file );
  2268. output( "%s: tools/wmc include dummy\n", pot_path );
  2269. output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
  2270. strarray_add( &pot_files, pot_path );
  2271. }
  2272. }
  2273. }
  2274. if (linguas.count)
  2275. {
  2276. for (i = 0; i < linguas.count; i++)
  2277. output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
  2278. output( ": %s/wine.pot\n", po_dir );
  2279. output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
  2280. po_dir );
  2281. output( "po:" );
  2282. for (i = 0; i < linguas.count; i++)
  2283. output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
  2284. output( "\n" );
  2285. }
  2286. output( "%s/wine.pot:", po_dir );
  2287. output_filenames( pot_files );
  2288. output( "\n" );
  2289. output( "\tmsgcat -o $@" );
  2290. output_filenames( pot_files );
  2291. output( "\n" );
  2292. }
  2293. /*******************************************************************
  2294. * output_source_y
  2295. */
  2296. static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
  2297. {
  2298. /* add source file dependency for parallel makes */
  2299. char *header = strmake( "%s.tab.h", obj );
  2300. if (find_include_file( make, header ))
  2301. {
  2302. output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
  2303. output( "\t%s -p %s_ -o %s.tab.c -d %s\n",
  2304. bison, obj, obj_dir_path( make, obj ), source->filename );
  2305. output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
  2306. source->filename, obj_dir_path( make, header ));
  2307. strarray_add( &make->clean_files, header );
  2308. }
  2309. else output( "%s.tab.c: %s\n", obj, source->filename );
  2310. output( "\t%s -p %s_ -o $@ %s\n", bison, obj, source->filename );
  2311. }
  2312. /*******************************************************************
  2313. * output_source_l
  2314. */
  2315. static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
  2316. {
  2317. output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
  2318. output( "\t%s -o$@ %s\n", flex, source->filename );
  2319. }
  2320. /*******************************************************************
  2321. * output_source_h
  2322. */
  2323. static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
  2324. {
  2325. if (source->file->flags & FLAG_GENERATED)
  2326. strarray_add( &make->all_targets, source->name );
  2327. else
  2328. add_install_rule( make, source->name, source->name,
  2329. strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
  2330. }
  2331. /*******************************************************************
  2332. * output_source_rc
  2333. */
  2334. static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
  2335. {
  2336. struct strarray defines = get_source_defines( make, source, obj );
  2337. char *po_dir = NULL;
  2338. unsigned int i;
  2339. if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
  2340. if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = top_obj_dir_path( make, "po" );
  2341. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2342. if (source->file->flags & FLAG_RC_PO && !(source->file->flags & FLAG_PARENTDIR))
  2343. {
  2344. strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
  2345. output( "%s.pot ", obj_dir_path( make, obj ) );
  2346. }
  2347. output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
  2348. output_filename( tools_path( make, "wrc" ));
  2349. output_filenames( source->dependencies );
  2350. output( "\n" );
  2351. output( "\t%s -u -o $@", tools_path( make, "wrc" ) );
  2352. if (make->is_win16) output_filename( "-m16" );
  2353. else output_filenames( target_flags );
  2354. output_filename( "--nostdinc" );
  2355. if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
  2356. output_filenames( defines );
  2357. output_filename( source->filename );
  2358. output( "\n" );
  2359. if (po_dir)
  2360. {
  2361. output( "%s.res:", obj_dir_path( make, obj ));
  2362. for (i = 0; i < linguas.count; i++)
  2363. output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
  2364. output( "\n" );
  2365. }
  2366. }
  2367. /*******************************************************************
  2368. * output_source_mc
  2369. */
  2370. static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
  2371. {
  2372. unsigned int i;
  2373. char *obj_path = obj_dir_path( make, obj );
  2374. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2375. strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
  2376. output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
  2377. output_filename( tools_path( make, "wmc" ));
  2378. output_filenames( source->dependencies );
  2379. output( "\n" );
  2380. output( "\t%s -u -o $@ %s", tools_path( make, "wmc" ), source->filename );
  2381. if (linguas.count)
  2382. {
  2383. char *po_dir = top_obj_dir_path( make, "po" );
  2384. output_filename( strmake( "--po-dir=%s", po_dir ));
  2385. output( "\n" );
  2386. output( "%s.res:", obj_dir_path( make, obj ));
  2387. for (i = 0; i < linguas.count; i++)
  2388. output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
  2389. }
  2390. output( "\n" );
  2391. }
  2392. /*******************************************************************
  2393. * output_source_res
  2394. */
  2395. static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
  2396. {
  2397. strarray_add( &make->res_files, source->name );
  2398. }
  2399. /*******************************************************************
  2400. * output_source_idl
  2401. */
  2402. static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
  2403. {
  2404. struct strarray defines = get_source_defines( make, source, obj );
  2405. struct strarray targets = empty_strarray;
  2406. char *dest;
  2407. unsigned int i;
  2408. if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
  2409. if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
  2410. for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
  2411. {
  2412. if (!(source->file->flags & idl_outputs[i].flag)) continue;
  2413. dest = strmake( "%s%s", obj, idl_outputs[i].ext );
  2414. if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
  2415. strarray_add( &targets, dest );
  2416. }
  2417. if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
  2418. if (source->file->flags & FLAG_INSTALL)
  2419. {
  2420. add_install_rule( make, source->name, xstrdup( source->name ),
  2421. strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
  2422. if (source->file->flags & FLAG_IDL_HEADER)
  2423. add_install_rule( make, source->name, strmake( "%s.h", obj ),
  2424. strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
  2425. }
  2426. if (!targets.count) return;
  2427. output_filenames_obj_dir( make, targets );
  2428. output( ": %s\n", tools_path( make, "widl" ));
  2429. output( "\t%s -o $@", tools_path( make, "widl" ) );
  2430. output_filenames( target_flags );
  2431. output_filename( "--nostdinc" );
  2432. output_filenames( defines );
  2433. output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
  2434. output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
  2435. output_filename( source->filename );
  2436. output( "\n" );
  2437. output_filenames_obj_dir( make, targets );
  2438. output( ": %s", source->filename );
  2439. output_filenames( source->dependencies );
  2440. output( "\n" );
  2441. }
  2442. /*******************************************************************
  2443. * output_source_tlb
  2444. */
  2445. static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
  2446. {
  2447. strarray_add( &make->all_targets, source->name );
  2448. }
  2449. /*******************************************************************
  2450. * output_source_x
  2451. */
  2452. static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
  2453. {
  2454. output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
  2455. tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
  2456. output( "\t%s%s -H -o $@ %s\n",
  2457. tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
  2458. if (source->file->flags & FLAG_INSTALL)
  2459. {
  2460. add_install_rule( make, source->name, source->name,
  2461. strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
  2462. add_install_rule( make, source->name, strmake( "%s.h", obj ),
  2463. strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
  2464. }
  2465. }
  2466. /*******************************************************************
  2467. * output_source_sfd
  2468. */
  2469. static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
  2470. {
  2471. unsigned int i;
  2472. char *ttf_obj = strmake( "%s.ttf", obj );
  2473. char *ttf_file = src_dir_path( make, ttf_obj );
  2474. if (fontforge && !make->src_dir)
  2475. {
  2476. output( "%s: %s\n", ttf_file, source->filename );
  2477. output( "\t%s -script %s %s $@\n",
  2478. fontforge, top_src_dir_path( make, "fonts/genttf.ff" ), source->filename );
  2479. if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
  2480. }
  2481. if (source->file->flags & FLAG_INSTALL)
  2482. {
  2483. add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
  2484. output_srcdir_symlink( make, ttf_obj );
  2485. }
  2486. if (source->file->flags & FLAG_SFD_FONTS)
  2487. {
  2488. struct strarray *array = source->file->args;
  2489. for (i = 0; i < array->count; i++)
  2490. {
  2491. char *font = strtok( xstrdup(array->str[i]), " \t" );
  2492. char *args = strtok( NULL, "" );
  2493. strarray_add( &make->all_targets, xstrdup( font ));
  2494. output( "%s: %s %s\n", obj_dir_path( make, font ),
  2495. tools_path( make, "sfnt2fon" ), ttf_file );
  2496. output( "\t%s -q -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
  2497. add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
  2498. }
  2499. }
  2500. }
  2501. /*******************************************************************
  2502. * output_source_svg
  2503. */
  2504. static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
  2505. {
  2506. static const char * const images[] = { "bmp", "cur", "ico", NULL };
  2507. unsigned int i;
  2508. if (convert && rsvg && icotool && !make->src_dir)
  2509. {
  2510. for (i = 0; images[i]; i++)
  2511. if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
  2512. if (images[i])
  2513. {
  2514. output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
  2515. output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
  2516. top_src_dir_path( make, "tools/buildimage" ), source->filename );
  2517. }
  2518. }
  2519. }
  2520. /*******************************************************************
  2521. * output_source_nls
  2522. */
  2523. static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
  2524. {
  2525. add_install_rule( make, source->name, source->name,
  2526. strmake( "D$(nlsdir)/%s", source->name ));
  2527. output_srcdir_symlink( make, strmake( "%s.nls", obj ));
  2528. }
  2529. /*******************************************************************
  2530. * output_source_desktop
  2531. */
  2532. static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
  2533. {
  2534. add_install_rule( make, source->name, source->name,
  2535. strmake( "D$(datadir)/applications/%s", source->name ));
  2536. }
  2537. /*******************************************************************
  2538. * output_source_po
  2539. */
  2540. static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
  2541. {
  2542. output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
  2543. output( "\t%s -o $@ %s\n", msgfmt, source->filename );
  2544. strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
  2545. }
  2546. /*******************************************************************
  2547. * output_source_in
  2548. */
  2549. static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
  2550. {
  2551. unsigned int i;
  2552. if (strendswith( obj, ".man" ) && source->file->args)
  2553. {
  2554. struct strarray symlinks;
  2555. char *dir, *dest = replace_extension( obj, ".man", "" );
  2556. char *lang = strchr( dest, '.' );
  2557. char *section = source->file->args;
  2558. if (lang)
  2559. {
  2560. *lang++ = 0;
  2561. dir = strmake( "$(mandir)/%s/man%s", lang, section );
  2562. }
  2563. else dir = strmake( "$(mandir)/man%s", section );
  2564. add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
  2565. symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
  2566. for (i = 0; i < symlinks.count; i++)
  2567. add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
  2568. strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
  2569. free( dest );
  2570. free( dir );
  2571. }
  2572. strarray_add( &make->in_files, xstrdup(obj) );
  2573. strarray_add( &make->all_targets, xstrdup(obj) );
  2574. output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
  2575. output( "\t%s %s >$@ || (rm -f $@ && false)\n", sed_cmd, source->filename );
  2576. output( "%s:", obj_dir_path( make, obj ));
  2577. output_filenames( source->dependencies );
  2578. output( "\n" );
  2579. add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
  2580. }
  2581. /*******************************************************************
  2582. * output_source_spec
  2583. */
  2584. static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
  2585. {
  2586. struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
  2587. struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
  2588. struct strarray all_libs, dep_libs = empty_strarray;
  2589. char *dll_name, *obj_name;
  2590. if (!imports.count) imports = make->imports;
  2591. if (!dll_flags.count) dll_flags = make->extradllflags;
  2592. all_libs = add_import_libs( make, &dep_libs, imports, 0 );
  2593. add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
  2594. dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
  2595. obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
  2596. strarray_add( &make->clean_files, dll_name );
  2597. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2598. output( "%s.res: %s\n", obj_dir_path( make, obj ), obj_dir_path( make, dll_name ));
  2599. output( "\techo \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", obj,
  2600. obj_dir_path( make, dll_name ), tools_path( make, "wrc" ));
  2601. output( "%s:", obj_dir_path( make, dll_name ));
  2602. output_filename( source->filename );
  2603. output_filename( obj_name );
  2604. output_filenames( dep_libs );
  2605. output_filename( tools_path( make, "winebuild" ));
  2606. output_filename( tools_path( make, "winegcc" ));
  2607. output( "\n" );
  2608. output_winegcc_command( make );
  2609. output_filename( "-s" );
  2610. output_filenames( dll_flags );
  2611. output_filename( "-shared" );
  2612. output_filename( source->filename );
  2613. output_filename( obj_name );
  2614. output_filenames( all_libs );
  2615. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  2616. output( "\n" );
  2617. }
  2618. /*******************************************************************
  2619. * output_source_default
  2620. */
  2621. static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
  2622. {
  2623. struct strarray defines = get_source_defines( make, source, obj );
  2624. int is_dll_src = (make->testdll &&
  2625. strendswith( source->name, ".c" ) &&
  2626. find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
  2627. int need_cross = (crosstarget &&
  2628. !(source->file->flags & FLAG_C_UNIX) &&
  2629. (make->is_cross ||
  2630. ((source->file->flags & FLAG_C_IMPLIB) &&
  2631. (needs_cross_lib( make ) || needs_delay_lib( make ))) ||
  2632. (make->staticlib && needs_cross_lib( make ))));
  2633. int need_obj = ((*dll_ext || !make->staticlib || !(source->file->flags & FLAG_C_UNIX)) &&
  2634. (!need_cross ||
  2635. (source->file->flags & FLAG_C_IMPLIB) ||
  2636. (make->module && make->staticlib)));
  2637. if ((source->file->flags & FLAG_GENERATED) &&
  2638. (!make->testdll || !strendswith( source->filename, "testlist.c" )))
  2639. strarray_add( &make->clean_files, source->filename );
  2640. if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
  2641. if (need_obj)
  2642. {
  2643. if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
  2644. strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
  2645. else if (!is_dll_src && (!(source->file->flags & FLAG_C_IMPLIB) || (make->importlib && strarray_exists( &make->imports, make->importlib ))))
  2646. strarray_add( &make->object_files, strmake( "%s.o", obj ));
  2647. else
  2648. strarray_add( &make->clean_files, strmake( "%s.o", obj ));
  2649. output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
  2650. output( "\t$(CC) -c -o $@ %s", source->filename );
  2651. output_filenames( defines );
  2652. if (make->module || make->staticlib || make->sharedlib || make->testdll)
  2653. {
  2654. output_filenames( dll_flags );
  2655. if (source->use_msvcrt) output_filenames( msvcrt_flags );
  2656. }
  2657. output_filenames( extra_cflags );
  2658. output_filenames( make->extraincl_args );
  2659. output_filenames( cpp_flags );
  2660. output_filename( "$(CFLAGS)" );
  2661. output( "\n" );
  2662. }
  2663. if (need_cross)
  2664. {
  2665. strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
  2666. output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
  2667. output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
  2668. output_filenames( defines );
  2669. output_filenames( extra_cross_cflags );
  2670. if (source->file->flags & FLAG_C_IMPLIB) output_filename( "-fno-builtin" );
  2671. output_filenames( cpp_flags );
  2672. output_filename( "$(CROSSCFLAGS)" );
  2673. output( "\n" );
  2674. }
  2675. if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
  2676. {
  2677. strarray_add( &make->c2man_files, source->filename );
  2678. if (make->testdll && !is_dll_src)
  2679. {
  2680. strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
  2681. output( "%s.ok:\n", obj_dir_path( make, obj ));
  2682. output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
  2683. top_src_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ),
  2684. make->testdll, replace_extension( make->testdll, ".dll", "_test.exe" ),
  2685. make->is_cross ? "" : dll_ext, obj );
  2686. }
  2687. }
  2688. if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
  2689. if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
  2690. output( ":" );
  2691. output_filenames( source->dependencies );
  2692. output( "\n" );
  2693. }
  2694. /* dispatch table to output rules for a single source file */
  2695. static const struct
  2696. {
  2697. const char *ext;
  2698. void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
  2699. } output_source_funcs[] =
  2700. {
  2701. { "y", output_source_y },
  2702. { "l", output_source_l },
  2703. { "h", output_source_h },
  2704. { "rh", output_source_h },
  2705. { "inl", output_source_h },
  2706. { "rc", output_source_rc },
  2707. { "mc", output_source_mc },
  2708. { "res", output_source_res },
  2709. { "idl", output_source_idl },
  2710. { "tlb", output_source_tlb },
  2711. { "sfd", output_source_sfd },
  2712. { "svg", output_source_svg },
  2713. { "nls", output_source_nls },
  2714. { "desktop", output_source_desktop },
  2715. { "po", output_source_po },
  2716. { "in", output_source_in },
  2717. { "x", output_source_x },
  2718. { "spec", output_source_spec },
  2719. { NULL, output_source_default }
  2720. };
  2721. /*******************************************************************
  2722. * has_object_file
  2723. */
  2724. static int has_object_file( struct makefile *make )
  2725. {
  2726. struct incl_file *source;
  2727. int i;
  2728. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  2729. {
  2730. char *ext = get_extension( source->name );
  2731. if (!ext) fatal_error( "unsupported file type %s\n", source->name );
  2732. ext++;
  2733. for (i = 0; output_source_funcs[i].ext; i++)
  2734. if (!strcmp( ext, output_source_funcs[i].ext )) break;
  2735. if (!output_source_funcs[i].ext) return 1; /* default extension builds to an object file */
  2736. }
  2737. return 0;
  2738. }
  2739. /*******************************************************************
  2740. * output_man_pages
  2741. */
  2742. static void output_man_pages( struct makefile *make )
  2743. {
  2744. if (make->c2man_files.count)
  2745. {
  2746. char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2747. output( "manpages::\n" );
  2748. output( "\t%s -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
  2749. output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
  2750. output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
  2751. output_filename( strmake( "-o %s/man%s",
  2752. top_obj_dir_path( make, "documentation" ), man_ext ));
  2753. output_filenames( make->c2man_files );
  2754. output( "\n" );
  2755. output( "htmlpages::\n" );
  2756. output( "\t%s -Th -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
  2757. output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
  2758. output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
  2759. output_filename( strmake( "-o %s",
  2760. top_obj_dir_path( make, "documentation/html" )));
  2761. output_filenames( make->c2man_files );
  2762. output( "\n" );
  2763. output( "sgmlpages::\n" );
  2764. output( "\t%s -Ts -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
  2765. output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
  2766. output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
  2767. output_filename( strmake( "-o %s",
  2768. top_obj_dir_path( make, "documentation/api-guide" )));
  2769. output_filenames( make->c2man_files );
  2770. output( "\n" );
  2771. output( "xmlpages::\n" );
  2772. output( "\t%s -Tx -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
  2773. output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
  2774. output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
  2775. output_filename( strmake( "-o %s",
  2776. top_obj_dir_path( make, "documentation/api-guide-xml" )));
  2777. output_filenames( make->c2man_files );
  2778. output( "\n" );
  2779. strarray_add( &make->phony_targets, "manpages" );
  2780. strarray_add( &make->phony_targets, "htmlpages" );
  2781. strarray_add( &make->phony_targets, "sgmlpages" );
  2782. strarray_add( &make->phony_targets, "xmlpages" );
  2783. }
  2784. else output( "manpages htmlpages sgmlpages xmlpages::\n" );
  2785. }
  2786. /*******************************************************************
  2787. * output_module
  2788. */
  2789. static void output_module( struct makefile *make )
  2790. {
  2791. struct strarray all_libs = empty_strarray;
  2792. struct strarray dep_libs = empty_strarray;
  2793. char *module_path = obj_dir_path( make, make->module );
  2794. char *spec_file = NULL;
  2795. unsigned int i;
  2796. if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2797. strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1 ));
  2798. strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0 ));
  2799. add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
  2800. if (make->is_cross)
  2801. {
  2802. if (delay_load_flag)
  2803. {
  2804. for (i = 0; i < make->delayimports.count; i++)
  2805. strarray_add( &all_libs, strmake( "%s%s%s", delay_load_flag, make->delayimports.str[i],
  2806. strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
  2807. }
  2808. strarray_add( &make->all_targets, strmake( "%s", make->module ));
  2809. add_install_rule( make, make->module, strmake( "%s", make->module ),
  2810. strmake( "c$(dlldir)/%s", make->module ));
  2811. output( "%s:", module_path );
  2812. }
  2813. else
  2814. {
  2815. strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
  2816. if (*dll_ext)
  2817. {
  2818. for (i = 0; i < make->delayimports.count; i++)
  2819. strarray_add( &all_libs, strmake( "-Wl,-delayload,%s%s", make->delayimports.str[i],
  2820. strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
  2821. strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
  2822. strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
  2823. add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
  2824. strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
  2825. add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
  2826. strmake( "d$(dlldir)/fakedlls/%s", make->module ));
  2827. output( "%s%s %s.fake:", module_path, dll_ext, module_path );
  2828. }
  2829. else
  2830. {
  2831. strarray_add( &make->all_targets, make->module );
  2832. add_install_rule( make, make->module, make->module,
  2833. strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
  2834. output( "%s:", module_path );
  2835. }
  2836. }
  2837. if (spec_file) output_filename( spec_file );
  2838. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  2839. output_filenames_obj_dir( make, make->res_files );
  2840. output_filenames( dep_libs );
  2841. output_filename( tools_path( make, "winebuild" ));
  2842. output_filename( tools_path( make, "winegcc" ));
  2843. output( "\n" );
  2844. output_winegcc_command( make );
  2845. if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
  2846. if (spec_file)
  2847. {
  2848. output_filename( "-shared" );
  2849. output_filename( spec_file );
  2850. }
  2851. output_filenames( make->extradllflags );
  2852. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  2853. output_filenames_obj_dir( make, make->res_files );
  2854. output_filenames( all_libs );
  2855. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  2856. output( "\n" );
  2857. if (spec_file && make->importlib)
  2858. {
  2859. char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
  2860. if (*dll_ext && !make->implib_objs.count)
  2861. {
  2862. strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
  2863. output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
  2864. output( "\t%s -w --def -o $@", tools_path( make, "winebuild" ) );
  2865. output_filenames( target_flags );
  2866. if (make->is_win16) output_filename( "-m16" );
  2867. output_filename( "--export" );
  2868. output_filename( spec_file );
  2869. output( "\n" );
  2870. add_install_rule( make, make->importlib,
  2871. strmake( "lib%s.def", make->importlib ),
  2872. strmake( "d$(dlldir)/lib%s.def", make->importlib ));
  2873. }
  2874. else
  2875. {
  2876. strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
  2877. if (!*dll_ext && needs_delay_lib( make ))
  2878. {
  2879. strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
  2880. output( "%s.delay.a ", importlib_path );
  2881. }
  2882. output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
  2883. output_filenames_obj_dir( make, make->implib_objs );
  2884. output( "\n" );
  2885. output( "\t%s -w --implib -o $@", tools_path( make, "winebuild" ) );
  2886. output_filenames( target_flags );
  2887. if (make->is_win16) output_filename( "-m16" );
  2888. output_filename( "--export" );
  2889. output_filename( spec_file );
  2890. output_filenames_obj_dir( make, make->implib_objs );
  2891. output( "\n" );
  2892. add_install_rule( make, make->importlib,
  2893. strmake( "lib%s.a", make->importlib ),
  2894. strmake( "d$(dlldir)/lib%s.a", make->importlib ));
  2895. }
  2896. if (crosstarget && (needs_cross_lib( make ) || needs_delay_lib( make )))
  2897. {
  2898. struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
  2899. if (needs_cross_lib( make ))
  2900. {
  2901. strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
  2902. output_filename( strmake( "%s.cross.a", importlib_path ));
  2903. }
  2904. if (needs_delay_lib( make ))
  2905. {
  2906. strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
  2907. output_filename( strmake( "%s.delay.a", importlib_path ));
  2908. }
  2909. output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
  2910. output_filenames_obj_dir( make, cross_files );
  2911. output( "\n" );
  2912. output( "\t%s -b %s -w --implib -o $@", tools_path( make, "winebuild" ), crosstarget );
  2913. if (make->is_win16) output_filename( "-m16" );
  2914. output_filename( "--export" );
  2915. output_filename( spec_file );
  2916. output_filenames_obj_dir( make, cross_files );
  2917. output( "\n" );
  2918. }
  2919. }
  2920. if (spec_file)
  2921. output_man_pages( make );
  2922. else if (*dll_ext && !make->is_win16 && strendswith( make->module, ".exe" ))
  2923. {
  2924. char *binary = replace_extension( make->module, ".exe", "" );
  2925. add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
  2926. }
  2927. }
  2928. /*******************************************************************
  2929. * output_static_lib
  2930. */
  2931. static void output_static_lib( struct makefile *make )
  2932. {
  2933. strarray_add( &make->all_targets, make->staticlib );
  2934. output( "%s:", obj_dir_path( make, make->staticlib ));
  2935. output_filenames_obj_dir( make, make->object_files );
  2936. output_filenames_obj_dir( make, make->unixobj_files );
  2937. output( "\n\trm -f $@\n" );
  2938. output( "\t%s rc $@", ar );
  2939. output_filenames_obj_dir( make, make->object_files );
  2940. output_filenames_obj_dir( make, make->unixobj_files );
  2941. output( "\n\t%s $@\n", ranlib );
  2942. add_install_rule( make, make->staticlib, make->staticlib,
  2943. strmake( "d$(dlldir)/%s", make->staticlib ));
  2944. if (needs_cross_lib( make ))
  2945. {
  2946. char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
  2947. strarray_add( &make->all_targets, name );
  2948. output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
  2949. output_filenames_obj_dir( make, make->crossobj_files );
  2950. output( "\n" );
  2951. output( "\t%s -b %s -w --staticlib -o $@", tools_path( make, "winebuild" ), crosstarget );
  2952. output_filenames_obj_dir( make, make->crossobj_files );
  2953. output( "\n" );
  2954. }
  2955. }
  2956. /*******************************************************************
  2957. * output_shared_lib
  2958. */
  2959. static void output_shared_lib( struct makefile *make )
  2960. {
  2961. unsigned int i;
  2962. char *basename, *p;
  2963. struct strarray names = get_shared_lib_names( make->sharedlib );
  2964. struct strarray all_libs = empty_strarray;
  2965. struct strarray dep_libs = empty_strarray;
  2966. basename = xstrdup( make->sharedlib );
  2967. if ((p = strchr( basename, '.' ))) *p = 0;
  2968. strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
  2969. strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
  2970. strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
  2971. output( "%s:", obj_dir_path( make, make->sharedlib ));
  2972. output_filenames_obj_dir( make, make->object_files );
  2973. output_filenames( dep_libs );
  2974. output( "\n" );
  2975. output( "\t$(CC) -o $@" );
  2976. output_filenames_obj_dir( make, make->object_files );
  2977. output_filenames( all_libs );
  2978. output_filename( "$(LDFLAGS)" );
  2979. output( "\n" );
  2980. add_install_rule( make, make->sharedlib, make->sharedlib,
  2981. strmake( "p$(libdir)/%s", make->sharedlib ));
  2982. for (i = 1; i < names.count; i++)
  2983. {
  2984. output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
  2985. output_symlink_rule( obj_dir_path( make, names.str[i-1] ), obj_dir_path( make, names.str[i] ));
  2986. add_install_rule( make, names.str[i], names.str[i-1],
  2987. strmake( "y$(libdir)/%s", names.str[i] ));
  2988. }
  2989. strarray_addall( &make->all_targets, names );
  2990. }
  2991. /*******************************************************************
  2992. * output_test_module
  2993. */
  2994. static void output_test_module( struct makefile *make )
  2995. {
  2996. char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
  2997. char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
  2998. char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
  2999. struct strarray dep_libs = empty_strarray;
  3000. struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0 );
  3001. struct makefile *parent = get_parent_makefile( make );
  3002. const char *ext = make->is_cross ? "" : dll_ext;
  3003. const char *parent_ext = parent && parent->is_cross ? "" : dll_ext;
  3004. add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
  3005. strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
  3006. strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
  3007. output( "%s%s:\n", obj_dir_path( make, testmodule ), ext );
  3008. output_winegcc_command( make );
  3009. output_filenames( make->extradllflags );
  3010. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3011. output_filenames_obj_dir( make, make->res_files );
  3012. output_filenames( all_libs );
  3013. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  3014. output( "\n" );
  3015. output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
  3016. output_winegcc_command( make );
  3017. output_filename( "-s" );
  3018. output_filename( strmake( "-Wb,-F,%s", testmodule ));
  3019. output_filenames( make->extradllflags );
  3020. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3021. output_filenames_obj_dir( make, make->res_files );
  3022. output_filenames( all_libs );
  3023. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  3024. output( "\n" );
  3025. output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
  3026. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3027. output_filenames_obj_dir( make, make->res_files );
  3028. output_filenames( dep_libs );
  3029. output_filename( tools_path( make, "winebuild" ));
  3030. output_filename( tools_path( make, "winegcc" ));
  3031. output( "\n" );
  3032. if (!make->disabled && !strarray_exists( &disabled_dirs, "programs/winetest" ))
  3033. output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
  3034. output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
  3035. obj_dir_path( make, stripped ), ext );
  3036. output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n",
  3037. testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
  3038. output_filenames_obj_dir( make, make->ok_files );
  3039. output( ": %s%s ../%s%s\n", testmodule, ext, make->testdll, parent_ext );
  3040. output( "check test:" );
  3041. if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
  3042. output( "\n" );
  3043. strarray_add( &make->phony_targets, "check" );
  3044. strarray_add( &make->phony_targets, "test" );
  3045. output( "testclean::\n" );
  3046. output( "\trm -f" );
  3047. output_filenames_obj_dir( make, make->ok_files );
  3048. output( "\n" );
  3049. strarray_addall( &make->clean_files, make->ok_files );
  3050. strarray_add( &make->phony_targets, "testclean" );
  3051. }
  3052. /*******************************************************************
  3053. * output_programs
  3054. */
  3055. static void output_programs( struct makefile *make )
  3056. {
  3057. unsigned int i, j;
  3058. char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
  3059. char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
  3060. for (i = 0; i < make->programs.count; i++)
  3061. {
  3062. char *program_installed = NULL;
  3063. char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
  3064. struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
  3065. struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
  3066. struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
  3067. struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
  3068. if (!objs.count) objs = make->object_files;
  3069. strarray_addall( &all_libs, add_default_libraries( make, &deps ));
  3070. output( "%s:", obj_dir_path( make, program ) );
  3071. output_filenames_obj_dir( make, objs );
  3072. output_filenames( deps );
  3073. output( "\n" );
  3074. output( "\t$(CC) -o $@" );
  3075. output_filenames_obj_dir( make, objs );
  3076. if (strarray_exists( &all_libs, "-lwine" ))
  3077. {
  3078. strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
  3079. if (ldrpath_local && ldrpath_install)
  3080. {
  3081. program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
  3082. output_filename( ldrpath_local );
  3083. output_filenames( all_libs );
  3084. output_filename( "$(LDFLAGS)" );
  3085. output( "\n" );
  3086. output( "%s:", obj_dir_path( make, program_installed ) );
  3087. output_filenames_obj_dir( make, objs );
  3088. output_filenames( deps );
  3089. output( "\n" );
  3090. output( "\t$(CC) -o $@" );
  3091. output_filenames_obj_dir( make, objs );
  3092. output_filename( ldrpath_install );
  3093. strarray_add( &make->all_targets, program_installed );
  3094. }
  3095. }
  3096. output_filenames( all_libs );
  3097. output_filename( "$(LDFLAGS)" );
  3098. output( "\n" );
  3099. strarray_add( &make->all_targets, program );
  3100. for (j = 0; j < symlinks.count; j++)
  3101. {
  3102. output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
  3103. output_symlink_rule( obj_dir_path( make, program ), obj_dir_path( make, symlinks.str[j] ));
  3104. }
  3105. strarray_addall( &make->all_targets, symlinks );
  3106. add_install_rule( make, program, program_installed ? program_installed : program,
  3107. strmake( "p$(bindir)/%s", program ));
  3108. for (j = 0; j < symlinks.count; j++)
  3109. add_install_rule( make, symlinks.str[j], program,
  3110. strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
  3111. }
  3112. }
  3113. /*******************************************************************
  3114. * output_subdirs
  3115. */
  3116. static void output_subdirs( struct makefile *make )
  3117. {
  3118. struct strarray symlinks = empty_strarray;
  3119. struct strarray all_deps = empty_strarray;
  3120. struct strarray build_deps = empty_strarray;
  3121. struct strarray builddeps_deps = empty_strarray;
  3122. struct strarray makefile_deps = empty_strarray;
  3123. struct strarray clean_files = empty_strarray;
  3124. struct strarray testclean_files = empty_strarray;
  3125. struct strarray distclean_files = empty_strarray;
  3126. struct strarray tools_deps = empty_strarray;
  3127. struct strarray tooldeps_deps = empty_strarray;
  3128. struct strarray winetest_deps = empty_strarray;
  3129. unsigned int i, j;
  3130. strarray_addall( &distclean_files, make->distclean_files );
  3131. for (i = 0; i < make->subdirs.count; i++)
  3132. {
  3133. const struct makefile *submake = make->submakes[i];
  3134. char *subdir = base_dir_path( submake, "" );
  3135. strarray_add( &makefile_deps, top_src_dir_path( make, base_dir_path( submake,
  3136. strmake ( "%s.in", output_makefile_name ))));
  3137. for (j = 0; j < submake->clean_files.count; j++)
  3138. strarray_add( &clean_files, base_dir_path( submake, submake->clean_files.str[j] ));
  3139. for (j = 0; j < submake->distclean_files.count; j++)
  3140. strarray_add( &distclean_files, base_dir_path( submake, submake->distclean_files.str[j] ));
  3141. for (j = 0; j < submake->ok_files.count; j++)
  3142. strarray_add( &testclean_files, base_dir_path( submake, submake->ok_files.str[j] ));
  3143. /* import libs are still created for disabled dirs, except for win16 ones */
  3144. if (submake->module && submake->importlib && !(submake->disabled && submake->is_win16))
  3145. {
  3146. char *importlib_path = base_dir_path( submake, strmake( "lib%s", submake->importlib ));
  3147. if (submake->implib_objs.count)
  3148. {
  3149. output( "%s.a: dummy\n", importlib_path );
  3150. output( "\t@cd %s && $(MAKE) lib%s.a\n", subdir, submake->importlib );
  3151. strarray_add( &tools_deps, strmake( "%s.a", importlib_path ));
  3152. if (needs_cross_lib( submake ))
  3153. {
  3154. output( "%s.cross.a: dummy\n", importlib_path );
  3155. output( "\t@cd %s && $(MAKE) lib%s.cross.a\n", subdir, submake->importlib );
  3156. strarray_add( &tools_deps, strmake( "%s.cross.a", importlib_path ));
  3157. }
  3158. if (needs_delay_lib( submake ))
  3159. {
  3160. output( "%s.delay.a: dummy\n", importlib_path );
  3161. output( "\t@cd %s && $(MAKE) lib%s.delay.a\n", subdir, submake->importlib );
  3162. strarray_add( &tools_deps, strmake( "%s.delay.a", importlib_path ));
  3163. }
  3164. }
  3165. else
  3166. {
  3167. char *spec_file = top_src_dir_path( make, base_dir_path( submake,
  3168. replace_extension( submake->module, ".dll", ".spec" )));
  3169. if (*dll_ext)
  3170. {
  3171. output( "%s.def: %s", importlib_path, spec_file );
  3172. strarray_add( &build_deps, strmake( "%s.def", importlib_path ));
  3173. }
  3174. else
  3175. {
  3176. if (needs_delay_lib( submake ))
  3177. {
  3178. output( "%s.delay.a ", importlib_path );
  3179. strarray_add( &build_deps, strmake( "%s.delay.a", importlib_path ));
  3180. }
  3181. output( "%s.a: %s", importlib_path, spec_file );
  3182. strarray_add( &build_deps, strmake( "%s.a", importlib_path ));
  3183. }
  3184. output_filename( tools_path( make, "winebuild" ));
  3185. output( "\n" );
  3186. output( "\t%s -w -o $@", tools_path( make, "winebuild" ));
  3187. output_filename( *dll_ext ? "--def" : "--implib" );
  3188. output_filenames( target_flags );
  3189. if (submake->is_win16) output_filename( "-m16" );
  3190. output_filename( "--export" );
  3191. output_filename( spec_file );
  3192. output( "\n" );
  3193. if (crosstarget && (needs_cross_lib( submake ) || needs_delay_lib( submake )))
  3194. {
  3195. if (needs_cross_lib( submake ))
  3196. {
  3197. output_filename( strmake( "%s.cross.a", importlib_path ));
  3198. strarray_add( &build_deps, strmake( "%s.cross.a", importlib_path ));
  3199. }
  3200. if (needs_delay_lib( submake ))
  3201. {
  3202. output_filename( strmake( "%s.delay.a", importlib_path ));
  3203. strarray_add( &build_deps, strmake( "%s.delay.a", importlib_path ));
  3204. }
  3205. output( ": %s", spec_file );
  3206. output_filename( tools_path( make, "winebuild" ));
  3207. output( "\n" );
  3208. output( "\t%s -b %s -w -o $@", tools_path( make, "winebuild" ), crosstarget );
  3209. if (submake->is_win16) output_filename( "-m16" );
  3210. output_filename( "--implib" );
  3211. output_filename( "--export" );
  3212. output_filename( spec_file );
  3213. output( "\n" );
  3214. }
  3215. }
  3216. strarray_addall( &symlinks, output_importlib_symlinks( make, submake ));
  3217. }
  3218. if (submake->disabled) continue;
  3219. strarray_add( &all_deps, subdir );
  3220. if (submake->module)
  3221. {
  3222. if (!submake->staticlib)
  3223. {
  3224. strarray_add( &builddeps_deps, subdir );
  3225. if (!submake->is_exe)
  3226. {
  3227. output( "manpages htmlpages sgmlpages xmlpages::\n" );
  3228. output( "\t@cd %s && $(MAKE) $@\n", subdir );
  3229. }
  3230. }
  3231. else strarray_add( &tools_deps, subdir );
  3232. }
  3233. else if (submake->testdll)
  3234. {
  3235. output( "%s/test: dummy\n", subdir );
  3236. output( "\t@cd %s && $(MAKE) test\n", subdir );
  3237. strarray_add( &winetest_deps, subdir );
  3238. strarray_add( &builddeps_deps, subdir );
  3239. }
  3240. else
  3241. {
  3242. if (!strcmp( submake->base_dir, "tools" ) || !strncmp( submake->base_dir, "tools/", 6 ))
  3243. {
  3244. strarray_add( &tooldeps_deps, submake->base_dir );
  3245. for (j = 0; j < submake->programs.count; j++)
  3246. output( "%s/%s%s: %s\n", submake->base_dir,
  3247. submake->programs.str[j], tools_ext, submake->base_dir );
  3248. }
  3249. if (submake->programs.count || submake->sharedlib)
  3250. {
  3251. struct strarray libs = get_expanded_make_var_array( submake, "EXTRALIBS" );
  3252. for (j = 0; j < submake->programs.count; j++)
  3253. strarray_addall( &libs, get_expanded_file_local_var( submake,
  3254. submake->programs.str[j], "LDFLAGS" ));
  3255. output( "%s: libs/port", submake->base_dir );
  3256. for (j = 0; j < libs.count; j++)
  3257. {
  3258. if (!strcmp( libs.str[j], "-lwpp" )) output_filename( "libs/wpp" );
  3259. if (!strcmp( libs.str[j], "-lwine" )) output_filename( "libs/wine" );
  3260. }
  3261. output( "\n" );
  3262. }
  3263. }
  3264. if (submake->install_rules[INSTALL_LIB].count)
  3265. {
  3266. output( "install install-lib:: %s\n", submake->base_dir );
  3267. output_install_commands( make, submake, submake->install_rules[INSTALL_LIB] );
  3268. }
  3269. if (submake->install_rules[INSTALL_DEV].count)
  3270. {
  3271. output( "install install-dev:: %s\n", submake->base_dir );
  3272. output_install_commands( make, submake, submake->install_rules[INSTALL_DEV] );
  3273. }
  3274. }
  3275. output( "all:" );
  3276. output_filenames( all_deps );
  3277. output( "\n" );
  3278. output_filenames( all_deps );
  3279. output( ": dummy\n" );
  3280. output( "\t@cd $@ && $(MAKE)\n" );
  3281. output( "Makefile:" );
  3282. output_filenames( makefile_deps );
  3283. output( "\n" );
  3284. output_filenames( makefile_deps );
  3285. output( ":\n" );
  3286. if (tooldeps_deps.count)
  3287. {
  3288. output( "__tooldeps__:" );
  3289. output_filenames( tooldeps_deps );
  3290. output( "\n" );
  3291. strarray_add( &make->phony_targets, "__tooldeps__" );
  3292. }
  3293. if (winetest_deps.count)
  3294. {
  3295. output( "buildtests programs/winetest:" );
  3296. output_filenames( winetest_deps );
  3297. output( "\n" );
  3298. output( "check test:" );
  3299. for (i = 0; i < winetest_deps.count; i++)
  3300. {
  3301. char *target = strmake( "%s/test", winetest_deps.str[i] );
  3302. output_filename( target );
  3303. strarray_add( &make->phony_targets, target );
  3304. }
  3305. output( "\n" );
  3306. strarray_add( &make->phony_targets, "buildtests" );
  3307. strarray_add( &make->phony_targets, "check" );
  3308. strarray_add( &make->phony_targets, "test" );
  3309. }
  3310. output( "clean::\n");
  3311. output_rm_filenames( clean_files );
  3312. output( "testclean::\n");
  3313. output_rm_filenames( testclean_files );
  3314. output( "distclean::\n");
  3315. output_rm_filenames( distclean_files );
  3316. output_filenames( tools_deps );
  3317. output( ":" );
  3318. output_filename( tools_dir_path( make, "widl" ));
  3319. output_filename( tools_dir_path( make, "winebuild" ));
  3320. output_filename( tools_dir_path( make, "winegcc" ));
  3321. output_filename( obj_dir_path( make, "include" ));
  3322. output( "\n" );
  3323. output_filenames( builddeps_deps );
  3324. output( ": __builddeps__\n" );
  3325. strarray_add( &make->phony_targets, "distclean" );
  3326. strarray_add( &make->phony_targets, "testclean" );
  3327. strarray_addall( &make->phony_targets, all_deps );
  3328. strarray_addall( &make->clean_files, symlinks );
  3329. strarray_addall( &build_deps, tools_deps );
  3330. strarray_addall( &build_deps, symlinks );
  3331. if (build_deps.count)
  3332. {
  3333. output( "__builddeps__:" );
  3334. output_filenames( build_deps );
  3335. output( "\n" );
  3336. strarray_add( &make->phony_targets, "__builddeps__" );
  3337. }
  3338. if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
  3339. }
  3340. /*******************************************************************
  3341. * output_sources
  3342. */
  3343. static void output_sources( struct makefile *make )
  3344. {
  3345. struct incl_file *source;
  3346. unsigned int i, j;
  3347. strarray_add( &make->phony_targets, "all" );
  3348. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  3349. {
  3350. char *obj = xstrdup( source->name );
  3351. char *ext = get_extension( obj );
  3352. if (!ext) fatal_error( "unsupported file type %s\n", source->name );
  3353. *ext++ = 0;
  3354. for (j = 0; output_source_funcs[j].ext; j++)
  3355. if (!strcmp( ext, output_source_funcs[j].ext )) break;
  3356. output_source_funcs[j].fn( make, source, obj );
  3357. strarray_addall_uniq( &make->dependencies, source->dependencies );
  3358. }
  3359. /* special case for winetest: add resource files from other test dirs */
  3360. if (make->base_dir && !strcmp( make->base_dir, "programs/winetest" ))
  3361. {
  3362. struct strarray tests = enable_tests;
  3363. if (!tests.count)
  3364. for (i = 0; i < top_makefile->subdirs.count; i++)
  3365. if (top_makefile->submakes[i]->testdll && !top_makefile->submakes[i]->disabled)
  3366. strarray_add( &tests, top_makefile->submakes[i]->testdll );
  3367. for (i = 0; i < tests.count; i++)
  3368. strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
  3369. }
  3370. if (make->dlldata_files.count)
  3371. {
  3372. output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
  3373. tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
  3374. output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
  3375. output_filenames( make->dlldata_files );
  3376. output( "\n" );
  3377. }
  3378. if (make->staticlib) output_static_lib( make );
  3379. else if (make->module) output_module( make );
  3380. else if (make->testdll) output_test_module( make );
  3381. else if (make->sharedlib) output_shared_lib( make );
  3382. else if (make->programs.count) output_programs( make );
  3383. for (i = 0; i < make->scripts.count; i++)
  3384. add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
  3385. strmake( "S$(bindir)/%s", make->scripts.str[i] ));
  3386. if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
  3387. strarray_add( &make->distclean_files, "Makefile" );
  3388. if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
  3389. if (!make->base_dir)
  3390. strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
  3391. else if (!strcmp( make->base_dir, "po" ))
  3392. strarray_add( &make->distclean_files, "LINGUAS" );
  3393. if (make->subdirs.count) output_subdirs( make );
  3394. if (!make->disabled)
  3395. {
  3396. if (make->all_targets.count)
  3397. {
  3398. output( "all:" );
  3399. output_filenames_obj_dir( make, make->all_targets );
  3400. output( "\n" );
  3401. }
  3402. output_install_rules( make, INSTALL_LIB, "install-lib" );
  3403. output_install_rules( make, INSTALL_DEV, "install-dev" );
  3404. output_uninstall_rules( make );
  3405. }
  3406. if (make->dependencies.count)
  3407. {
  3408. output_filenames( make->dependencies );
  3409. output( ":\n" );
  3410. }
  3411. strarray_addall( &make->clean_files, make->object_files );
  3412. strarray_addall( &make->clean_files, make->crossobj_files );
  3413. strarray_addall( &make->clean_files, make->unixobj_files );
  3414. strarray_addall( &make->clean_files, make->res_files );
  3415. strarray_addall( &make->clean_files, make->all_targets );
  3416. strarray_addall( &make->clean_files, make->extra_targets );
  3417. if (make->clean_files.count)
  3418. {
  3419. output( "%s::\n", obj_dir_path( make, "clean" ));
  3420. output( "\trm -f" );
  3421. output_filenames_obj_dir( make, make->clean_files );
  3422. output( "\n" );
  3423. if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
  3424. strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
  3425. }
  3426. if (make->phony_targets.count)
  3427. {
  3428. output( ".PHONY:" );
  3429. output_filenames( make->phony_targets );
  3430. output( "\n" );
  3431. }
  3432. }
  3433. /*******************************************************************
  3434. * create_temp_file
  3435. */
  3436. static FILE *create_temp_file( const char *orig )
  3437. {
  3438. char *name = xmalloc( strlen(orig) + 13 );
  3439. unsigned int i, id = getpid();
  3440. int fd;
  3441. FILE *ret = NULL;
  3442. for (i = 0; i < 100; i++)
  3443. {
  3444. sprintf( name, "%s.tmp%08x", orig, id );
  3445. if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
  3446. {
  3447. ret = fdopen( fd, "w" );
  3448. break;
  3449. }
  3450. if (errno != EEXIST) break;
  3451. id += 7777;
  3452. }
  3453. if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
  3454. temp_file_name = name;
  3455. return ret;
  3456. }
  3457. /*******************************************************************
  3458. * rename_temp_file
  3459. */
  3460. static void rename_temp_file( const char *dest )
  3461. {
  3462. int ret = rename( temp_file_name, dest );
  3463. if (ret == -1 && errno == EEXIST)
  3464. {
  3465. /* rename doesn't overwrite on windows */
  3466. unlink( dest );
  3467. ret = rename( temp_file_name, dest );
  3468. }
  3469. if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
  3470. temp_file_name = NULL;
  3471. }
  3472. /*******************************************************************
  3473. * are_files_identical
  3474. */
  3475. static int are_files_identical( FILE *file1, FILE *file2 )
  3476. {
  3477. for (;;)
  3478. {
  3479. char buffer1[8192], buffer2[8192];
  3480. int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
  3481. int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
  3482. if (size1 != size2) return 0;
  3483. if (!size1) return feof( file1 ) && feof( file2 );
  3484. if (memcmp( buffer1, buffer2, size1 )) return 0;
  3485. }
  3486. }
  3487. /*******************************************************************
  3488. * rename_temp_file_if_changed
  3489. */
  3490. static void rename_temp_file_if_changed( const char *dest )
  3491. {
  3492. FILE *file1, *file2;
  3493. int do_rename = 1;
  3494. if ((file1 = fopen( dest, "r" )))
  3495. {
  3496. if ((file2 = fopen( temp_file_name, "r" )))
  3497. {
  3498. do_rename = !are_files_identical( file1, file2 );
  3499. fclose( file2 );
  3500. }
  3501. fclose( file1 );
  3502. }
  3503. if (!do_rename)
  3504. {
  3505. unlink( temp_file_name );
  3506. temp_file_name = NULL;
  3507. }
  3508. else rename_temp_file( dest );
  3509. }
  3510. /*******************************************************************
  3511. * output_linguas
  3512. */
  3513. static void output_linguas( const struct makefile *make )
  3514. {
  3515. const char *dest = base_dir_path( make, "LINGUAS" );
  3516. struct incl_file *source;
  3517. output_file = create_temp_file( dest );
  3518. output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
  3519. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  3520. if (strendswith( source->name, ".po" ))
  3521. output( "%s\n", replace_extension( source->name, ".po", "" ));
  3522. if (fclose( output_file )) fatal_perror( "write" );
  3523. output_file = NULL;
  3524. rename_temp_file_if_changed( dest );
  3525. }
  3526. /*******************************************************************
  3527. * output_testlist
  3528. */
  3529. static void output_testlist( const struct makefile *make )
  3530. {
  3531. const char *dest = base_dir_path( make, "testlist.c" );
  3532. struct strarray files = empty_strarray;
  3533. unsigned int i;
  3534. for (i = 0; i < make->ok_files.count; i++)
  3535. strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
  3536. output_file = create_temp_file( dest );
  3537. output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
  3538. output( "#define WIN32_LEAN_AND_MEAN\n" );
  3539. output( "#include <windows.h>\n\n" );
  3540. output( "#define STANDALONE\n" );
  3541. output( "#include \"wine/test.h\"\n\n" );
  3542. for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
  3543. output( "\n" );
  3544. output( "const struct test winetest_testlist[] =\n" );
  3545. output( "{\n" );
  3546. for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
  3547. output( " { 0, 0 }\n" );
  3548. output( "};\n" );
  3549. if (fclose( output_file )) fatal_perror( "write" );
  3550. output_file = NULL;
  3551. rename_temp_file_if_changed( dest );
  3552. }
  3553. /*******************************************************************
  3554. * output_gitignore
  3555. */
  3556. static void output_gitignore( const char *dest, struct strarray files )
  3557. {
  3558. int i;
  3559. output_file = create_temp_file( dest );
  3560. output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
  3561. for (i = 0; i < files.count; i++)
  3562. {
  3563. if (!strchr( files.str[i], '/' )) output( "/" );
  3564. output( "%s\n", files.str[i] );
  3565. }
  3566. if (fclose( output_file )) fatal_perror( "write" );
  3567. output_file = NULL;
  3568. rename_temp_file( dest );
  3569. }
  3570. /*******************************************************************
  3571. * output_top_variables
  3572. */
  3573. static void output_top_variables( const struct makefile *make )
  3574. {
  3575. unsigned int i;
  3576. struct strarray *vars = &top_makefile->vars;
  3577. if (!make->base_dir) return; /* don't output variables in the top makefile */
  3578. output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
  3579. output( "all:\n\n" );
  3580. for (i = 0; i < vars->count; i += 2)
  3581. {
  3582. if (!strcmp( vars->str[i], "SUBDIRS" )) continue; /* not inherited */
  3583. output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
  3584. }
  3585. output( "\n" );
  3586. }
  3587. /*******************************************************************
  3588. * output_dependencies
  3589. */
  3590. static void output_dependencies( struct makefile *make )
  3591. {
  3592. struct strarray ignore_files = empty_strarray;
  3593. char buffer[1024];
  3594. FILE *src_file;
  3595. int found = 0;
  3596. if (make->base_dir) create_dir( make->base_dir );
  3597. output_file_name = base_dir_path( make, output_makefile_name );
  3598. output_file = create_temp_file( output_file_name );
  3599. output_top_variables( make );
  3600. /* copy the contents of the source makefile */
  3601. src_file = open_input_makefile( make );
  3602. while (fgets( buffer, sizeof(buffer), src_file ) && !found)
  3603. {
  3604. if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
  3605. found = !strncmp( buffer, separator, strlen(separator) );
  3606. }
  3607. if (fclose( src_file )) fatal_perror( "close" );
  3608. input_file_name = NULL;
  3609. if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
  3610. output_sources( make );
  3611. fclose( output_file );
  3612. output_file = NULL;
  3613. rename_temp_file( output_file_name );
  3614. strarray_addall( &ignore_files, make->distclean_files );
  3615. strarray_addall( &ignore_files, make->clean_files );
  3616. if (make->testdll) output_testlist( make );
  3617. if (make->base_dir && !strcmp( make->base_dir, "po" )) output_linguas( make );
  3618. if (!make->src_dir) output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
  3619. create_file_directories( make, ignore_files );
  3620. output_file_name = NULL;
  3621. }
  3622. /*******************************************************************
  3623. * load_sources
  3624. */
  3625. static void load_sources( struct makefile *make )
  3626. {
  3627. static const char *source_vars[] =
  3628. {
  3629. "SOURCES",
  3630. "C_SRCS",
  3631. "OBJC_SRCS",
  3632. "RC_SRCS",
  3633. "MC_SRCS",
  3634. "IDL_SRCS",
  3635. "BISON_SRCS",
  3636. "LEX_SRCS",
  3637. "HEADER_SRCS",
  3638. "XTEMPLATE_SRCS",
  3639. "SVG_SRCS",
  3640. "FONT_SRCS",
  3641. "IN_SRCS",
  3642. "PO_SRCS",
  3643. "MANPAGES",
  3644. NULL
  3645. };
  3646. const char **var, *crt_dll = NULL;
  3647. unsigned int i;
  3648. struct strarray value;
  3649. struct incl_file *file;
  3650. if (root_src_dir)
  3651. {
  3652. make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
  3653. make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
  3654. }
  3655. strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
  3656. strarray_set_value( &make->vars, "top_srcdir", top_src_dir_path( make, "" ));
  3657. strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
  3658. make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
  3659. make->module = get_expanded_make_variable( make, "MODULE" );
  3660. make->testdll = get_expanded_make_variable( make, "TESTDLL" );
  3661. make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
  3662. make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
  3663. make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
  3664. make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
  3665. make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
  3666. make->imports = get_expanded_make_var_array( make, "IMPORTS" );
  3667. make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
  3668. make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
  3669. make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
  3670. make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
  3671. make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
  3672. if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
  3673. make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
  3674. if ((make->module && make->staticlib) || make->testdll || make->is_win16)
  3675. strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
  3676. strarray_addall( &make->extradllflags, get_expanded_make_var_array( make, "APPMODE" ));
  3677. make->disabled = make->base_dir && strarray_exists( &disabled_dirs, make->base_dir );
  3678. make->use_msvcrt = strarray_exists( &make->extradllflags, "-mno-cygwin" );
  3679. make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
  3680. strarray_exists( &make->extradllflags, "-mwindows" );
  3681. if (make->module && !make->install_lib.count && !make->install_dev.count)
  3682. {
  3683. if (make->importlib) strarray_add( &make->install_dev, make->importlib );
  3684. if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
  3685. else strarray_add( &make->install_lib, make->module );
  3686. }
  3687. make->include_paths = empty_strarray;
  3688. make->include_args = empty_strarray;
  3689. make->define_args = empty_strarray;
  3690. make->extraincl_args = empty_strarray;
  3691. strarray_add( &make->define_args, "-D__WINESRC__" );
  3692. value = get_expanded_make_var_array( make, "EXTRAINCL" );
  3693. for (i = 0; i < value.count; i++)
  3694. if (!strncmp( value.str[i], "-I", 2 ))
  3695. strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
  3696. else
  3697. strarray_add_uniq( &make->extraincl_args, value.str[i] );
  3698. strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
  3699. strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
  3700. if (make->src_dir)
  3701. strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
  3702. if (make->parent_dir)
  3703. strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
  3704. strarray_add( &make->include_args, strmake( "-I%s", top_obj_dir_path( make, "include" )));
  3705. if (make->top_src_dir)
  3706. strarray_add( &make->include_args, strmake( "-I%s", top_src_dir_path( make, "include" )));
  3707. list_init( &make->sources );
  3708. list_init( &make->includes );
  3709. for (var = source_vars; *var; var++)
  3710. {
  3711. value = get_expanded_make_var_array( make, *var );
  3712. for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
  3713. }
  3714. add_generated_sources( make );
  3715. if (!make->use_msvcrt && !has_object_file( make ))
  3716. {
  3717. strarray_add( &make->extradllflags, "-mno-cygwin" );
  3718. make->use_msvcrt = 1;
  3719. }
  3720. if (make->use_msvcrt)
  3721. {
  3722. for (i = 0; i < make->imports.count; i++)
  3723. {
  3724. if (strncmp( make->imports.str[i], "msvcr", 5 ) && strncmp( make->imports.str[i], "ucrt", 4 )) continue;
  3725. if (crt_dll) fatal_error( "More than one crt DLL imported: %s %s\n", crt_dll, make->imports.str[i] );
  3726. crt_dll = make->imports.str[i];
  3727. }
  3728. if (!crt_dll && !strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
  3729. {
  3730. crt_dll = !make->testdll && !make->staticlib ? "ucrtbase" : "msvcrt";
  3731. strarray_add( &make->imports, crt_dll );
  3732. }
  3733. if (crt_dll && !strncmp( crt_dll, "ucrt", 4 )) strarray_add( &make->define_args, "-D_UCRT" );
  3734. }
  3735. LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
  3736. LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
  3737. make->is_cross = crosstarget && make->use_msvcrt;
  3738. if (make->is_cross)
  3739. {
  3740. for (i = 0; i < make->imports.count; i++)
  3741. strarray_add_uniq( &cross_import_libs, make->imports.str[i] );
  3742. if (make->is_win16) strarray_add_uniq( &cross_import_libs, "kernel" );
  3743. strarray_add_uniq( &cross_import_libs, "winecrt0" );
  3744. strarray_add_uniq( &cross_import_libs, "kernel32" );
  3745. strarray_add_uniq( &cross_import_libs, "ntdll" );
  3746. }
  3747. if (!*dll_ext || make->is_cross)
  3748. for (i = 0; i < make->delayimports.count; i++)
  3749. strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
  3750. }
  3751. /*******************************************************************
  3752. * parse_makeflags
  3753. */
  3754. static void parse_makeflags( const char *flags )
  3755. {
  3756. const char *p = flags;
  3757. char *var, *buffer = xmalloc( strlen(flags) + 1 );
  3758. while (*p)
  3759. {
  3760. while (isspace(*p)) p++;
  3761. var = buffer;
  3762. while (*p && !isspace(*p))
  3763. {
  3764. if (*p == '\\' && p[1]) p++;
  3765. *var++ = *p++;
  3766. }
  3767. *var = 0;
  3768. if (var > buffer) set_make_variable( &cmdline_vars, buffer );
  3769. }
  3770. }
  3771. /*******************************************************************
  3772. * parse_option
  3773. */
  3774. static int parse_option( const char *opt )
  3775. {
  3776. if (opt[0] != '-')
  3777. {
  3778. if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
  3779. return 0;
  3780. }
  3781. switch(opt[1])
  3782. {
  3783. case 'f':
  3784. if (opt[2]) output_makefile_name = opt + 2;
  3785. break;
  3786. case 'R':
  3787. relative_dir_mode = 1;
  3788. break;
  3789. default:
  3790. fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
  3791. exit(1);
  3792. }
  3793. return 1;
  3794. }
  3795. /*******************************************************************
  3796. * main
  3797. */
  3798. int main( int argc, char *argv[] )
  3799. {
  3800. const char *makeflags = getenv( "MAKEFLAGS" );
  3801. int i, j;
  3802. if (makeflags) parse_makeflags( makeflags );
  3803. i = 1;
  3804. while (i < argc)
  3805. {
  3806. if (parse_option( argv[i] ))
  3807. {
  3808. for (j = i; j < argc; j++) argv[j] = argv[j+1];
  3809. argc--;
  3810. }
  3811. else i++;
  3812. }
  3813. if (relative_dir_mode)
  3814. {
  3815. char *relpath;
  3816. if (argc != 3)
  3817. {
  3818. fprintf( stderr, "Option -R needs two directories\n%s", Usage );
  3819. exit( 1 );
  3820. }
  3821. relpath = get_relative_path( argv[1], argv[2] );
  3822. printf( "%s\n", relpath ? relpath : "." );
  3823. exit( 0 );
  3824. }
  3825. atexit( cleanup_files );
  3826. signal( SIGTERM, exit_on_signal );
  3827. signal( SIGINT, exit_on_signal );
  3828. #ifdef SIGHUP
  3829. signal( SIGHUP, exit_on_signal );
  3830. #endif
  3831. for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
  3832. top_makefile = parse_makefile( NULL );
  3833. target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
  3834. msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
  3835. dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
  3836. extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
  3837. extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
  3838. cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
  3839. lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
  3840. libs = get_expanded_make_var_array( top_makefile, "LIBS" );
  3841. enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
  3842. delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
  3843. top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
  3844. top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
  3845. root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
  3846. tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
  3847. tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
  3848. exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
  3849. man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
  3850. dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
  3851. crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
  3852. fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
  3853. convert = get_expanded_make_variable( top_makefile, "CONVERT" );
  3854. flex = get_expanded_make_variable( top_makefile, "FLEX" );
  3855. bison = get_expanded_make_variable( top_makefile, "BISON" );
  3856. ar = get_expanded_make_variable( top_makefile, "AR" );
  3857. ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
  3858. rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
  3859. icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
  3860. dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
  3861. msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
  3862. sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
  3863. ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
  3864. if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
  3865. if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
  3866. if (!exe_ext) exe_ext = "";
  3867. if (!tools_ext) tools_ext = "";
  3868. if (!man_ext) man_ext = "3w";
  3869. if (argc == 1)
  3870. {
  3871. disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
  3872. top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
  3873. top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );
  3874. for (i = 0; i < top_makefile->subdirs.count; i++)
  3875. top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i] );
  3876. load_sources( top_makefile );
  3877. for (i = 0; i < top_makefile->subdirs.count; i++)
  3878. load_sources( top_makefile->submakes[i] );
  3879. for (i = 0; i < top_makefile->subdirs.count; i++)
  3880. output_dependencies( top_makefile->submakes[i] );
  3881. output_dependencies( top_makefile );
  3882. return 0;
  3883. }
  3884. for (i = 1; i < argc; i++)
  3885. {
  3886. struct makefile *make = parse_makefile( argv[i] );
  3887. load_sources( make );
  3888. output_dependencies( make );
  3889. }
  3890. return 0;
  3891. }