makedep.c 150 KB

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