makedep.c 152 KB

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